Dictionary in Python
Dictionary in Python
Dictionary
Creating a dictionary
A dictionary is created by placing items, (i.e. key-value pair) inside curly braces.
Each key and value is separated by a colon and items separated by comma.
Here 1:’Aman’ is the first item in dictionary student. There are three items.
1, 2, 3 are the keys and Aman, Anandhu, Aravind are the values.
Empty dictionary
Eg: d={}
Or
Using dict() as
D= dict()
To create a dictionary
Student={1:”Aman”,2:”Arjun”,3:”Amit”}
print(Student)
Or
Student={ }
Student[1]=”Aman”
Student[2]=”Arjun”
Student[3]=”Amit”
print(Student)
Accessing elements in a dictionary
print(Dictionary_name[key])
Traversing a dictionary
Student={1:”Aman”,2:”Arjun”,3:”Amit”}
for i in Student:
student={1:”Aman”,2:”Arjun”,3:”Amit”}
student[4]=”Arav”
student[2]=”Alok”
student={1:”Aman”,2:”Arjun”,3:”Amit”}
Dictionary Functions
Two dictionaries can be merged into one by using update(). It merges the keys
and the values of one dictionary with another and overwrites the values of the
same key.
Syntax: dict_name1.update(dict_name2)
Eg 1: D1={1:10,2:20,3:30}
D2={4:40,5:50}
D1.update(D2)
Eg 2: D1={1:10,2:20,3:30}
D2={2:30,4:40}
D1.update(D2)
2) items()
D1={1:10,2:20,3:30}
D1.items()
3) keys()
D1={1:10,2:20,3:30}
D1.keys()
4) values()
D1={1:10,2:20,3:30}
D1.values()
Syntax: len(d)
6) fromkeys()
Syntax
Value is optional. If value is provided all the items will have the same value. If
not specified, value is set as None
Eg: seq=(1,2,3)
D1=dict.fromkeys(seq)
O/P: D1 ={1:None,2:None,3:None}
D1=dict.fromkeys(seq,20)
O/P: D1 ={1:20,2:20,3:20}
7) copy()
When copy() method is used, a new dictionary is created which is filled with a
copy of the references from the original dictionary.
new = original
new.clear()
print('new: ', new)
O/P: new: {}
original: {}
****************************************
new = original.copy()
new.clear()
O/P
new: {}
8) setdefault()
This method returns the value of the item with the specified key. If the key
does not exist, it inserts the key with the specified value.
Eg: D1={1:10,2:20,3:30}
X= D1.setdefault(1,”hello”)
print(X) O/P: 10
X= D1.setdefault(4,”hello”)
D1.setdefault(5)
D1={1:10,2:20,3:30}
min(D1) O/P: 1
• min(D1.values()) O/P: 10
Returns the lowest key with its value from the dictionary.
10) max()
D1={1:10,2:20,3:30}
max(D1) O/P: 3
• max(D1.values()) O/P: 30
Returns the highest key with its value from the dictionary.
11) sorted()
Syntax: sorted(dict_name)
get() method returns a value for the given key. If key not available, then
returns default value None.
Syntax: dict_name.get(key,default=None)
Default – value to be returned in case the key doesn’t exist. Or else returns
None.
Eg: d3={2:30,1:20,3:10}
d3.get(1) O/P: 20
d3.get(1,"Never") O/P: 20
1) del statement
Eg : D1={1:10,2:20,3:30}
del D1[2]
del d1
2) pop()
This method will delete an item from the dictionary and also return the
deleted value.
D1.pop(3) O/P: 30
This method removes the last item from dictionary and returns the deleted
item.
D1={1:10,2:20,3:30}
4) clear()
Syntax: d.clear()
D1={1:10,2:20,3:30}
D1.clear()
print(D1) O/P: {}