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 dict_items([(1, 'One'), (2, 'Two'), (3, 'Three')]) dict_items([('A', 'Apple'), ('B', 'Bat'), ('C', 'Cat'), ('D', 'Doll')])
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: dict_values(['One', 'Two', 'Three']) dict_values(['Apple', 'Bat', 'Cat', 'Doll'])
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
{1: 'Amit', 2: 'Sunil', 3: 'Three', 5: 'Lata', 6: 'Suman', 7: 'Ravi'}
#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'} ______________________________________________________________________________________________________________ B = {1: 'Amit', 2: 'Sunil', 5: 'Lata', 6: 'Suman', 7: 'Ravi'} a=B.pop(6) #It returns the element of Key - 6 print(a) print(B) OUTPUT Suman {1: 'Amit', 2: 'Sunil', 5: 'Lata', 7: 'Ravi'}
Assignment:
Python Dictionary Methods – Fill in the blanks:
- All the elements of dictionary are enclosed in ______________ bracket.
- Key and it’s value are separated by symbol ___________________ in dictionary.
- Dictionary is _________ data type(mapping/sequence).
- _________________ methods create an empty dictionary.
- __________ method delete all the elements of dictionary.
Python Dictionary Methods – True/False:
- Dictionary is mutable data type.
- Keys of dictionary must be unique.
- pop( ) returns the deleted element.
- length( ) function return the length of dictionary.
- D = { } – generate an empty string.
Python Dictionary Online Quiz
Total MCQ – 30
Total Marks – 30
Click here to start QUIZ-#1
Click here to start QUIZ-#2
Click here to start QUIZ-#3
Click for Online Quiz on String
Click for Online Quiz on List
Click for Online Quiz on Python Fundamental
Class 12 Computer Science Sample Paper 2020-2021.
Class 12 Computer Science Sample Paper Marking Scheme
Class 12 Computer Science Test Series