Python - Dictionary Data Structure
Python - Dictionary Data Structure
Table of Contents
1. Dictionary data structure ................................................................................................................... 2
2. When should we go for dictionary?................................................................................................... 3
3. Creating dictionary ............................................................................................................................. 4
4. Empty dictionary ................................................................................................................................ 4
5. We can add key-value pairs to empty dictionary.............................................................................. 5
6. Access values by using keys from dictionary ..................................................................................... 6
7. Update dictionary............................................................................................................................... 7
7.1.Case 1 ............................................................................................................................................ 7
7.2.Case 2 ............................................................................................................................................ 7
8. Removing or deleting elements from dictionary .............................................................................. 9
9. len(p) function .................................................................................................................................. 11
10. Methods in dict class data structure ............................................................................................. 12
10.1. clear() method ......................................................................................................................... 14
10.2. keys() method .......................................................................................................................... 15
10.3. values() ..................................................................................................................................... 15
10.4. items() ...................................................................................................................................... 16
11. Dictionary Comprehension: ........................................................................................................... 17
Note:
Create dictionary
Syntax
3. Creating dictionary
output
{10: "Ramesh", 20: "Arjun", 30: "Daniel"}
4. Empty dictionary
d = {}
print(d)
print(type(d))
output
{}
<class ‘dict’>
d = {}
d[10] = "Ramesh"
d[20] = "Arjun"
d[30] = "Daniel"
print(d)
output
{10: 'Ramesh', 20: 'Arjun', 30: 'Daniel'}
print(d[10])
print(d[20])
print(d[30])
output
Ramesh
Arjun
Daniel
Program Accessing key and value from dictionary using for loop
Name demo5.py
for k in d:
print(k, d[k])
output
10 Ramesh
20 Arjun
30 Daniel
7. Update dictionary
Syntax
d[key] = value
7.1.Case 1
✓ While updating the key in dictionary, if key is not available then a new
key will be added at the end of the dictionary with specified value.
7.2.Case 2
✓ While updating the key in dictionary, if key is already existing then old
value will be replaced with new value.
d[99] = "John"
print("Added key-value 99:John pair to dictionary: ", d)
output
Old dictionary: {10: 'Ramesh', 20: 'Arjun', 30: 'Daniel'}
d[30] = 'Chandhu'
print("Updated dictionary 3:Chandhu :", d)
output
Old dictionary: {10: 'Ramesh', 20: 'Arjun', 30: 'Daniel'}
Updated dictionary 3:Chandhu : {10: 'Ramesh', 20: 'Arjun', 30:
'Chandhu'}
Syntax
del d[key]
✓ As per the syntax, it deletes entry associated with the specified key.
✓ If the key is not available, then we will get KeyError
output
Before deleting key from dictionary:
{10: 'Ramesh', 20: 'Arjun', 30: 'Daniel'}
Syntax
del nameofthedictonary
output
{10: 'Ramesh', 20: 'Arjun', 30: 'Daniel'}
NameError: name ‘d’ is not defined
10 | P a g e 18.PYTHON DICTIONARY
Data Science – Python Dictionary Data Structure
9. len(p) function
✓ This function returns the number of items in the dictionary
output
length of dictionary is:2
11 | P a g e 18.PYTHON DICTIONARY
Data Science – Python Dictionary Data Structure
print(dir(dict))
output
[
'__class__', .......'__subclasshook__',
Important methods
12 | P a g e 18.PYTHON DICTIONARY
Data Science – Python Dictionary Data Structure
Important point
Important methods
✓ clear() method
✓ keys() method
✓ values() method
✓ items() method
13 | P a g e 18.PYTHON DICTIONARY
Data Science – Python Dictionary Data Structure
output
{10: 'Ramesh', 20: 'Arjun', 30: 'Daniel'}
After cleared entries in dictionary: {}
14 | P a g e 18.PYTHON DICTIONARY
Data Science – Python Dictionary Data Structure
Output
10.3. values()
Output
15 | P a g e 18.PYTHON DICTIONARY
Data Science – Python Dictionary Data Structure
10.4. items()
Program Accessing key and value from dictionary using items() method
Name demo15.py
for k, v in d.items():
print(k, v)
output
10 Ramesh
20 Arjun
30 Daniel
16 | P a g e 18.PYTHON DICTIONARY
Data Science – Python Dictionary Data Structure
Output
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
17 | P a g e 18.PYTHON DICTIONARY