Dictionaries



Creating a Dictionary

b={'name':'ABC', 'salary':1000,'age':25}

print(b)


Output

{'name': 'ABC', 'salary': 1000, 'age': 25}


Adding key value pairs to empty dictionary

e={}

e['name']="John"

e['salary']=10000

e[6]=25

print(e)


Output

{'name': 'John', 'salary': 10000, 6: 25}


Creating dictionary from name and value pairs

a=dict(name='ABC', salary=1000,age=25)

print(a)


Output

{'name': 'ABC', 'salary': 1000, 'age': 25}


Keys and values as separate tuples and zip() function

c=dict(zip(('name','salary',6),('ABC',10000,25)))

print(c)


Output

{'name': 'ABC', 'salary': 10000, 6: 25}


List or tuple of key:value pairs

x=dict([['name','John'],['salary',10000],[6,25]]) #Three list entries in one list

print(x)


Output

{'name': 'John', 'salary': 10000, 6: 25}


The fromkeys() method

e=dict.fromkeys([1,2,3,4,5],20)

print(e)


Output

{1: 20, 2: 20, 3: 20, 4: 20, 5: 20}



Working with Dictionaries


Adding elements to Dictionary

e={'name': 'John', 'salary': 10000, 'age': 25}

e['address']='101, B'

print(e)


Output

{'name': 'John', 'salary': 10000, 'age': 25, 'address': '101, B'}


Updating/Modifying existing elements in a Dictionary

e={'name': 'John', 'salary': 10000, 'age': 25}

e['salary']=20000

print(e)


Output

{'name': 'John', 'salary': 20000, 'age': 25}


Deleting elements from a Dictionary 

e={'name': 'John', 'salary': 10000, 'age': 25}

del e['salary']

print(e)


Output

{'name': 'John', 'age': 25}


Check for existence of a key

<key> in <dictionary>

e={'name': 'John', 'salary': 10000, 'age': 25}

print('age' in e)


Output

True


Pretty Printing in Dictionary

import json

a={'Nihar':3,'Rohan':4,'Zeba':5}

print(json.dumps(a,indent=2))


Output

{

  "Nihar": 3,

  "Rohan": 4,

  "Zeba": 5

}


Dictionary functions and methods 

len(<dictionary>)

e={'name': 'John', 'salary': 10000, 'age': 25}

print(len(e))


Output

3


<dictionary>.get(<key>)

You can get the item with the given key


e={'name': 'John', 'salary': 10000, 'age': 25}

print(e.get('name'))


Output

John


<dictionary>.items()

It will return all items of dictionary as a list of  (key,value) tuples


e={'name': 'John', 'salary': 10000, 'age': 25}

print(e.items())


Output

dict_items([('name', 'John'), ('salary', 10000), ('age', 25)])


e={'name': 'John', 'salary': 10000, 'age': 25}

l=e.items()

for x in l:

    print(x)


Output

('name', 'John')

('salary', 10000)

('age', 25)


<dictionary>.keys()

It will return all keys of dictionary as a list 

e={'name': 'John', 'salary': 10000, 'age': 25}

print(e.keys())


Output

['name', 'salary', 'age']


<dictionary>.values()

It will return all values of dictionary as a list 

e={'name': 'John', 'salary': 10000, 'age': 25}

print(e.values())


Output

['John', 10000, 25]


Extend/Update Dictionary with new key:value pairs

<dictionary>.setdefault()

e={'name': 'John', 'salary': 10000, 'age': 25}

e.setdefault('dept','Sales')

print(e)


Output

{'name': 'John', 'salary': 10000, 'age': 25, 'dept': 'Sales'}


<dictionary>.update()

e1={'name':'John','salary':10000,'age':24}

e2={'name':'Diya','salary':54000,'department':'Sales'}

e1.update(e2)

print(e1)


Output

{'name': 'Diya', 'salary': 54000, 'age': 24, 'department': 'Sales'}


<dictionary>.pop(key,value)

removes and returns the element associated with given key

a={1:'A',2:'B',3:'C',4:'D'}

a.pop(2)

print(a)


Output

{1: 'A', 3: 'C', 4: 'D'}


<dictionary>.popitem()

removes and returns the last dictionary element 

a={1:'A',2:'B',3:'C',4:'D'}

a.popitem()

print(a)


Output

{1: 'A', 2: 'B', 3: 'C'}


clear()

removes all items from the dictionary

a={1:'A',2:'B',3:'C',4:'D'}

a.clear()

print(a)


Output

{}


sorted(<dictionary>)

d1={1:'A',8:'D',3:'S',6:'E'}

b=sorted(d1)

c=sorted(d1.keys())

d=sorted(d1.values())

e=sorted(d1.items())

print(b)

print(c)

print(d)

print(e)


Output

[1, 3, 6, 8]

[1, 3, 6, 8]

['A', 'D', 'E', 'S']

[(1, 'A'), (3, 'S'), (6, 'E'), (8, 'D')]


min(), max() and sum()

d1={1:'A',8:'D',3:'S',6:'E'}

d2={(1,2,3,4,5):'A',(1,2,3):'E',(1,2,3,4,8):'C'}

d3={'A':8,'D':3,'S':6,'E':2}

print(min(d1))

print(max(d1))

print(sum(d1))

print()


print(min(d2))

print(max(d2))

print(min(d2.values()))

print(max(d2.values()))

print()


print(min(d3.values()))

print(max(d3.values()))

print(sum(d3.values()))

print()


Output

1

8

18


(1, 2, 3)

(1, 2, 3, 4, 8)

A

E


2

8

19

Comments

Popular Posts