Data Structure - Dictionary - Session10
Data Structure - Dictionary - Session10
Session -10
Dictionary
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
• Duplicate keys are not allowed but values can be duplicated.
• Heterogeneous objects are allowed for both key and values.
• Insertion order is not preserved.
• Dictionaries are mutable.
• Dictionaries are dynamic indexing and slicing concepts are not applicable.
Note: In C++ and Java Dictionaries are known as "Map" where as in Perl and
Ruby it is known as "Hash"
How to create Dictionary ?
d={} or d=dict()
d[100]="raman"
d[200]="ravi"
d[300]="shiva"
print(d) #{100: 'raman', 200: 'ravi', 300: 'shiva’}
print(d[100])
print(d[300])
If the specified key is not available then we will get KeyError
print(d[400])
if 400 in d:
print(d[400])
Write a program to enter name and percentage marks in a dictionary and
display information on the screen
d[key]=value
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:
d={100:"raman",200:"ravi",300:"shiva"}
print(d)
d[400]="tom"
print(d)
d[100]="jerry"
print(d)
How to delete elements from dictionary?
del d[key]
Eg:
d={100:"raman",200:"ravi",300:"shiva"}
print(d)
del d[100]
print(d)
del d[400]
d.clear()
To remove all entries from the dictionary
Eg:
d={100:"raman",200:"ravi",300:"shiva"}
print(d)
d.clear()
print(d)
del d
To delete total dictionary. Now we cannot access d
Eg:
d={100:"raman",200:"ravi",300:"shiva"}
print(d)
del d
print(d)
Important functions of dictionary:
1. dict():
To create a dictionary
2. len():
3. clear():
If the key is available then returns the corresponding value otherwise returns
None.
d.get(key,defaultvalue)
If the key is available then returns the corresponding value otherwise returns
default value.
d={100:"durga",200:"ravi",300:"shiva"}
print(d[100])
#print(d[400])
print(d.get(100))
print(d.get(400))
print(d.get(100,"Guest"))
print(d.get(400,"Guest"))
5. 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:
d={100:"raman",200:"ravi",300:"shiva"}
print(d.pop(100))
print(d)
print(d.pop(400))
6. popitem():
Eg:
d={100:"raman",200:"ravi",300:"shiva"}
print(d)
print(d.popitem())
print(d)
d={}
print(d.popitem()) ==>KeyError: 'popitem(): dictionary is empty'
7. keys():
d={100:"raman",200:"ravi",300:"shiva"}
print(d.keys())
for k in d.keys():
print(k)
8. values():
d={100:"raman",200:"ravi",300:"shiva"}
print(d.values())
for v in d.values():
print(v)
9. items():
[(k,v),(k,v),(k,v)]
Eg:
d={100:"raman",200:"ravi",300:"shiva"}
for k,v in d.items():
print(k,"--",v)
10. copy():
d1=d.copy();
11. 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.
12. update():
d.update(x)