CBSE Class 11 Python Dictionary Methods Comprehensive Notes with Practice Questions

Share with others

PYTHON DICTIONARY
Python Dictionary Methods

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

PYTHON DICTIONARY
Python Dictionary Methods

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')])
PYTHON DICTIONARY
Python Dictionary Methods

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

PYTHON DICTIONARY
Python Dictionary Methods

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:

  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.

Python Dictionary Online Quiz

Class 11 IP Chapter wise MCQ
Python Dictionary Methods

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


Share with others

Leave a Reply

%d bloggers like this: