Dictionary
Dictionary is a data structure in which we store values as a pair of key and value.
Each key is separated from its value by a colon (:), can consecutive items are
separated by commas.
Example 1
d={}
print(d)
print(type(d))
Output:
{}
<class 'dict'>
Dr. Shiladitya Chowdhury
Ph.D(Computer Science & Engineering)(JU)
1
Assistant Professor, Dept. of MCA
Techno Main Saltlake, Kolkata 700091
Example 2
d={"Name":"Raja Roy","Roll":11,"course":"MCA"}
print(d)
Output:
{'Name': 'Raja Roy', 'Roll': 11, 'course': 'MCA'}
Example 3
d=dict([("Name","Raja Roy"),("Roll",11),("course","MCA")])
print(d)
Output:
{'Name': 'Raja Roy', 'Roll': 11, 'course': 'MCA'}
Dr. Shiladitya Chowdhury
Ph.D(Computer Science & Engineering)(JU)
2
Assistant Professor, Dept. of MCA
Techno Main Saltlake, Kolkata 700091
Example 4
d={i:2*i for i in range(1,11)}
print(d)
Output:
{1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18, 10: 20}
Example 5
d={"Name":"Raja Roy","Roll":11,"course":"MCA"}
print("NAME:",d["Name"])
print("ROLL:",d["Roll"])
print("COURSE:",d["course"])
Output:
NAME: Raja Roy
ROLL: 11
Dr. Shiladitya Chowdhury
COURSE: MCA Ph.D(Computer Science & Engineering)(JU)
3
Assistant Professor, Dept. of MCA
Techno Main Saltlake, Kolkata 700091
Adding an item in a Dictionary
Example 6
d={"Name":"Raja Roy","Roll":11,"course":"MCA"}
print(d)
d["Marks"]=85
print(d)
Output:
{'Name': 'Raja Roy', 'Roll': 11, 'course': 'MCA'}
{'Name': 'Raja Roy', 'Roll': 11, 'course': 'MCA', 'Marks': 85}
Dr. Shiladitya Chowdhury
Ph.D(Computer Science & Engineering)(JU)
4
Assistant Professor, Dept. of MCA
Techno Main Saltlake, Kolkata 700091
Modifying an item in a Dictionary
Example 7
d={"Name":"Raja Roy","Roll":11,"course":"MCA"}
print(d)
d["course"]="BCA"
print(d)
Output:
{'Name': 'Raja Roy', 'Roll': 11, 'course': 'MCA'}
{'Name': 'Raja Roy', 'Roll': 11, 'course': 'BCA'}
Dr. Shiladitya Chowdhury
Ph.D(Computer Science & Engineering)(JU)
5
Assistant Professor, Dept. of MCA
Techno Main Saltlake, Kolkata 700091
Deleting an item from a Dictionary
We can delete one or more items using the del keyword.
To delete all the items in a single statement, we have to use clear() function.
To remove the directory from the memory, we have to use del statement del
directory_name.
Example 8A
d={"Name":"Raja Roy","Roll":11,"course":"MCA"}
print(d)
del d["course"]
print(d)
Output:
{'Name': 'Raja Roy', 'Roll': 11, 'course': 'MCA'}
{'Name': 'Raja Roy', 'Roll': 11}Dr. Shiladitya Chowdhury
Ph.D(Computer Science & Engineering)(JU)
6
Assistant Professor, Dept. of MCA
Techno Main Saltlake, Kolkata 700091
Example 8B
d={"Name":"Raja Roy","Roll":11,"course":"MCA"}
print(d)
[Link]()
print(d)
Output:
{'Name': 'Raja Roy', 'Roll': 11, 'course': 'MCA'}
{}
Dr. Shiladitya Chowdhury
Ph.D(Computer Science & Engineering)(JU)
7
Assistant Professor, Dept. of MCA
Techno Main Saltlake, Kolkata 700091
Example 8C
d={"Name":"Raja Roy","Roll":11,"course":"MCA"}
print(d)
del d
print(d)
Output:
{'Name': 'Raja Roy', 'Roll': 11, 'course': 'MCA'}
Traceback (most recent call last):
File "C:/Users/admin/PycharmProject/Dictionary/[Link]", line 4, in
<module>
print(d)
NameError: name 'd' is not defined
Dr. Shiladitya Chowdhury
Ph.D(Computer Science & Engineering)(JU)
8
Assistant Professor, Dept. of MCA
Techno Main Saltlake, Kolkata 700091
We can also use pop() method to delete a particular key from the dictionary.
Example 8D
d={"Name":"Raja Roy","Roll":11,"course":"MCA"}
print(d)
[Link]("Roll")
print(d)
Output:
{'Name': 'Raja Roy', 'Roll': 11, 'course': 'MCA'}
{'Name': 'Raja Roy', 'course': 'MCA'}
Keys must have unique values. If we try to add duplicate key, then the last assignment
is retained.
Dr. Shiladitya Chowdhury
Ph.D(Computer Science & Engineering)(JU)
9
Assistant Professor, Dept. of MCA
Techno Main Saltlake, Kolkata 700091
Example 9
d={"Name":"Raja Roy","Roll":11,"course":"MCA","Name":"Amit Das"}
print(d)
Output:
{'Name': 'Amit Das', 'Roll': 11, 'course': 'MCA'}
The in keyword can be used to check whether a single key is present in the directory.
Example 10
d1={"Name":"Raja Roy","Roll":11,"course":"MCA"}
if "course" in d1:
print(d1["course"])
d2={"Name":"Amit Das","Roll":15,"Gender":"Male"}
if "course" in d2:
print(d2["course"])
Output: Dr. Shiladitya Chowdhury
Ph.D(Computer Science & Engineering)(JU)
10
MCA Assistant Professor, Dept. of MCA
Techno Main Saltlake, Kolkata 700091
Loop over a Dictionary
Example 11
d={"Name":"Raja Roy","Roll":11,"course":"MCA"}
print(d)
print("Keys:")
for key in d:
print(key,end=' ')
print("\nValues:")
for val in [Link]():
print(val,end=' ')
print("\nDictionary:")
for key,val in [Link]():
print(key,val,end='; ')
Dr. Shiladitya Chowdhury
Ph.D(Computer Science & Engineering)(JU)
11
Assistant Professor, Dept. of MCA
Techno Main Saltlake, Kolkata 700091
Output:
{'Name': 'Raja Roy', 'Roll': 11, 'course': 'MCA'}
Keys:
Name Roll course
Values:
Raja Roy 11 MCA
Dictionary:
Name Raja Roy; Roll 11; course MCA;
Dr. Shiladitya Chowdhury
Ph.D(Computer Science & Engineering)(JU)
12
Assistant Professor, Dept. of MCA
Techno Main Saltlake, Kolkata 700091
Nested Dictionaries
We can define a directory, inside a directory.
Example 12
d={"Raja Roy":{"Phy":90,"Chem":92,"Math":98},
"Amit Das":{"Phy":80,"Chem":85,"Math":90},
"Amal Saha":{"Phy":75,"Chem":82,"Math":88}}
print(d)
for key,val in [Link]():
print(key,val,end='\n')
Dr. Shiladitya Chowdhury
Ph.D(Computer Science & Engineering)(JU)
13
Assistant Professor, Dept. of MCA
Techno Main Saltlake, Kolkata 700091
Output:
{'Raja Roy': {'Phy': 90, 'Chem': 92, 'Math': 98}, 'Amit Das': {'Phy': 80, 'Chem': 85, 'Math': 90},
'Amal Saha': {'Phy': 75, 'Chem': 82, 'Math': 88}}
Raja Roy {'Phy': 90, 'Chem': 92, 'Math': 98}
Amit Das {'Phy': 80, 'Chem': 85, 'Math': 90}
Amal Saha {'Phy': 75, 'Chem': 82, 'Math': 88}
Dr. Shiladitya Chowdhury
Ph.D(Computer Science & Engineering)(JU)
14
Assistant Professor, Dept. of MCA
Techno Main Saltlake, Kolkata 700091