Dictionary Revision
Dictionary Revision
COMPUTER SCIENCE
Python Dictionary Methods:
There are many methods of dictionary in python. Let’s do some of the important
function with example.
len( ) :
This function returns the length of Dictionary means, it returns the total number of
Key-Value pairs present in the dictionary. for example:
A = {1 : "One", 2 : "Two", 3 : "Three"}
B = {'A' : "Apple", 'B' : "Bat", 'C' : "Cat", 'D' : "Doll"}
print("Length of dictionary A is :", len(A))
print("Length of dictionary B is :", len(B))
OUTPUT
Length of dictionary A is : 3
Length of dictionary B is : 4
clear( ) :
This function simply removes all items from the dictionary. for example
A = {1 : "One", 2 : "Two", 3 : "Three"}
B = {'A' : "Apple", 'B' : "Bat", 'C' : "Cat", 'D' : "Doll"}
A.clear()
print("Length of dictionary A is :", len(A))
print("Length of dictionary B is :", len(B))
OUTPUT
Length of dictionary A is : 0
Length of dictionary B is : 4
NOTE : This function only remove element/items from the dictionary. It does not
delete the dictionary
get( ):
This function simply return the value of the required Key. for example
A = {1 : "One", 2 : "Two", 3 : "Three"}
B = {'A' : "Apple", 'B' : "Bat", 'C' : "Cat", 'D' : "Doll"}
print(A.get(2))
print(B.get('C'))
print(A.get(4)) #This key is not available in dictionary A so return NONE
print(A.get(4),"Key Not Found") #We can specify our message to display if key not
found
OUTPUT
Two
Cat
None
Key Not Found
NOTE: If key is not available in dictionary, then this method will return None or
customized message as shown above
items( ):
This function returns all the key value pair of dictionary in the form of list of tuples.
for example
A = {1 : "One", 2 : "Two", 3 : "Three"}
B = {'A' : "Apple", 'B' : "Bat", 'C' : "Cat", 'D' : "Doll"}
print(A.items())
print(B.items())
OUTPUT
Q1. What is the difference between print(A) and print(A.items( )) Where ‘A’ is
dictionary given above.
Ans. There is difference in the output of dictionary as print command print all key-
value pair in curly braces while itrms( ) return all key value pairs as a list of tuples. for
example
A = {1 : "One", 2 : "Two", 3 : "Three"}
print(A.items())
print(A)
OUTPUT:
dict_items([(1, 'One'), (2, 'Two'), (3, 'Three')])
{1: 'One', 2: 'Two', 3: 'Three'}
keys( ):
This function return all the keys of dictionary in the form of List. for example :
A = {1 : "One", 2 : "Two", 3 : "Three"}
B = {'A' : "Apple", 'B' : "Bat", 'C' : "Cat", 'D' : "Doll"}
print(A.keys())
print(B.keys())
OUTPUT:
dict_keys([1, 2, 3])
dict_keys(['A', 'B', 'C', 'D'])
values( ):
This function return all the values of dictionary in the form of List. for example :
A = {1 : "One", 2 : "Two", 3 : "Three"}
B = {'A' : "Apple", 'B' : "Bat", 'C' : "Cat", 'D' : "Doll"}
print(A.values())
print(B.values())
OUTPUT:
update( ):
This function merge the key value air of two dictionary into one. In simple words it
simply join/merge the two dictionary. It merge the keys and values of one dictionary
into other and overwrites the values of the same key. for example:
A = {1 : "One", 2 : "Two", 3 : "Three"}
B = {1: 'Amit', 2: 'Sunil', 5: 'Lata', 6: 'Suman', 7: 'Ravi'}
A.update(B)
print(A)
OUTPUT
#It over writes the values of same keys and add the values of different keys
pop( ):
This function not only delete the element of required key but also return the deleted
value.
B = {1: 'Amit', 2: 'Sunil', 5: 'Lata', 6: 'Suman', 7: 'Ravi'}
a=B.pop(2) #It returns the element of Key - 2
print(a)
print(B)
OUTPUT
Sunil
{1: 'Amit', 5: 'Lata', 6: 'Suman', 7: 'Ravi'}
____________________________________________________________________________________________________________
__
Suman
{1: 'Amit', 2: 'Sunil', 5: 'Lata', 7: 'Ravi'}
Assignment:
Python Dictionary Methods – Fill in the blanks:
1. All the elements of dictionary are enclosed in ______________ bracket.
2. Key and it’s value are separated by symbol ___________________ in dictionary.
3. Dictionary is _________ data type(mapping/sequence).
4. _________________ methods create an empty dictionary.
5. __________ method delete all the elements of dictionary.
Python Dictionary Methods – True/False:
1. Dictionary is mutable data type.
2. Keys of dictionary must be unique.
3. pop( ) returns the deleted element.
4. length( ) function return the length of dictionary.
5. D = { } – generate an empty string.