Dictionary Data Structure
Dictionary Data Structure
We can use List,Tuple and Set to represent a group of individual objects as a single entity.
Eg:
rollno----name
phone number--address
ipaddress---domain name
Note: In C++ and Java Dictionaries are known as "Map" where as in Perl and Ruby it is
known as "Hash"
d[100]="durga"
d[200]="ravi"
d[300]="shiva"
print(d) #{100: 'durga', 200: 'ravi', 300: 'shiva'}
d={key:value, key:value}
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
1 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
How to access data from the dictionary?
We can access data by using keys.
We can prevent this by checking whether key is already available or not by using
has_key() function or by using in operator.
But has_key() function is available only in Python 2 but not in Python 3. Hence
compulsory we have to use in operator.
if 400 in d:
print(d[400])
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
2 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
21) Enter % of Marks of Student: 80%
22) Name of Student % of marks
23) durga 60%
24) ravi 70 %
25) shiva 80%
If the key is not available then a new entry will be added to the dictionary with the
specified key-value pair
If the key is already available then old value will be replaced with new value.
Eg:
1. d={100:"durga",200:"ravi",300:"shiva"}
2. print(d)
3. d[400]="pavan"
4. print(d)
5. d[100]="sunny"
6. print(d)
7.
8. Output
9. {100: 'durga', 200: 'ravi', 300: 'shiva'}
10. {100: 'durga', 200: 'ravi', 300: 'shiva', 400: 'pavan'}
11. {100: 'sunny', 200: 'ravi', 300: 'shiva', 400: 'pavan'}
Eg:
1. d={100:"durga",200:"ravi",300:"shiva"}
2. print(d)
3. del d[100]
4. print(d)
5. del d[400]
6.
7. Output
8. {100: 'durga', 200: 'ravi', 300: 'shiva'}
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
3 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
9. {200: 'ravi', 300: 'shiva'}
10. KeyError: 400
d.clear()
Eg:
1. d={100:"durga",200:"ravi",300:"shiva"}
2. print(d)
3. d.clear()
4. print(d)
5.
6. Output
7. {100: 'durga', 200: 'ravi', 300: 'shiva'}
8. {}
del d
Eg:
1. d={100:"durga",200:"ravi",300:"shiva"}
2. print(d)
3. del d
4. print(d)
5.
6. Output
7. {100: 'durga', 200: 'ravi', 300: 'shiva'}
8. NameError: name 'd' is not defined
To create a dictionary
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
4 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
2. len()
3. clear():
4. get():
d.get(key)
If the key is available then returns the corresponding value otherwise returns None.It
wont raise any error.
d.get(key,defaultvalue)
If the key is available then returns the corresponding value otherwise returns default
value.
Eg:
d={100:"durga",200:"ravi",300:"shiva"}
print(d[100]) ==>durga
print(d[400]) ==>KeyError:400
print(d.get(100)) ==durga
print(d.get(400)) ==>None
print(d.get(100,"Guest")) ==durga
print(d.get(400,"Guest")) ==>Guest
3. pop():
d.pop(key)
It removes the entry associated with the specified key and returns the corresponding
value
If the specified key is not available then we will get KeyError
Eg:
1) d={100:"durga",200:"ravi",300:"shiva"}
2) print(d.pop(100))
3) print(d)
4) print(d.pop(400))
5)
6) Output
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
5 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
7) durga
8) {200: 'ravi', 300: 'shiva'}
9) KeyError: 400
4. popitem():
Eg:
1) d={100:"durga",200:"ravi",300:"shiva"}
2) print(d)
3) print(d.popitem())
4) print(d)
5)
6) Output
7) {100: 'durga', 200: 'ravi', 300: 'shiva'}
8) (300, 'shiva')
9) {100: 'durga', 200: 'ravi'}
5. keys():
Eg:
1) d={100:"durga",200:"ravi",300:"shiva"}
2) print(d.keys())
3) for k in d.keys():
4) print(k)
5)
6) Output
7) dict_keys([100, 200, 300])
8) 100
9) 200
10) 300
6. values():
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
6 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
Eg:
1. d={100:"durga",200:"ravi",300:"shiva"}
2. print(d.values())
3. for v in d.values():
4. print(v)
5.
6. Output
7. dict_values(['durga', 'ravi', 'shiva'])
8. durga
9. ravi
10. shiva
7. items():
[(k,v),(k,v),(k,v)]
Eg:
1. d={100:"durga",200:"ravi",300:"shiva"}
2. for k,v in d.items():
3. print(k,"--",v)
4.
5. Output
6. 100 -- durga
7. 200 -- ravi
8. 300 -- shiva
8. copy():
d1=d.copy();
9. setdefault():
d.setdefault(k,v)
If the key is already available then this function returns the corresponding value.
If the key is not available then the specified key-value will be added as new item to the
dictionary.
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
7 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
Eg:
1. d={100:"durga",200:"ravi",300:"shiva"}
2. print(d.setdefault(400,"pavan"))
3. print(d)
4. print(d.setdefault(100,"sachin"))
5. print(d)
6.
7. Output
8. pavan
9. {100: 'durga', 200: 'ravi', 300: 'shiva', 400: 'pavan'}
10. durga
11. {100: 'durga', 200: 'ravi', 300: 'shiva', 400: 'pavan'}
10. update():
d.update(x)
All items present in the dictionary x will be added to dictionary d
Q. Write a program to take dictionary from the keyboard and print the sum
of values?
1. d=eval(input("Enter dictionary:"))
2. s=sum(d.values())
3. print("Sum= ",s)
4.
5. Output
6. D:\Python_classes>py test.py
7. Enter dictionary:{'A':100,'B':200,'C':300}
8. Sum= 600
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
8 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com
14. p occurred 2 times
Q. Write a program to accept student name and marks from the keyboard
and creates a dictionary. Also display student marks by taking student name
as input?
Dictionary Comprehension:
Comprehension concept applicable for dictionaries also.
nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
10 040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | www.durgasoft.com