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}
1
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])
2
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'}
3
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
4
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
5
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():
6
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.
7
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
8
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?
9
24) Enter Student Name: banny
25) Enter Student Marks: 80
26) Enter Student Name: chinny
27) Enter Student Marks: 70
28) Enter Student Name: pinny
29) Enter Student Marks: 60
30) Enter Student Name: vinny
31) Enter Student Marks: 50
32) Enter Student Name to get Marks: sunny
33) The Marks of sunny are 90
34) Do you want to find another student marks[Yes|No]Yes
35) Enter Student Name to get Marks: durga
36) Student Not Found
37) Do you want to find another student marks[Yes|No]No
38) Thanks for using our application
Dictionary Comprehension:
Comprehension concept applicable for dictionaries also.
10