More Example of Dictionary pop() method
Let’s practice some of the use-case scenarios of python dictionary pop() method with some examples:
Example 1: Pop an element from the dictionary
Python3
# Python 3 code to demonstrate # working of pop() # initializing dictionary test_dict = { "Nikhil" : 7 , "Akshat" : 1 , "Akash" : 2 } # Printing initial dict print ( "The dictionary before deletion : " + str (test_dict)) # using pop to return and remove key-value pair. pop_ele = test_dict.pop( 'Akash' ) # Printing the value associated to popped key print ( "Value associated to popped key is : " + str (pop_ele)) # Printing dictionary after deletion print ( "Dictionary after deletion is : " + str (test_dict)) |
Output
The dictionary before deletion : {'Nikhil': 7, 'Akshat': 1, 'Akash': 2} Value associated to popped key is : 2 Dictionary after deletion is : {'Nikhil': 7, 'Akshat': 1}
Example 2: Python Dictionary pop() first element
Python3
# Python 3 code to demonstrate # working of pop() # initializing dictionary test_dict = { "Nikhil" : 7 , "Akshat" : 1 , "Akash" : 2 } # Printing initial dict print ( "The dictionary before deletion : " + str (test_dict)) # using pop to return and remove key-value pair. pop_first = test_dict.pop( "Nikhil" ) # Printing the value associated to popped key print ( "Value associated to popped key is : " + str (pop_first)) # Printing dictionary after deletion print ( "Dictionary after deletion is : " + str (test_dict)) |
Output
The dictionary before deletion : {'Nikhil': 7, 'Akshat': 1, 'Akash': 2} Value associated to popped key is : 7 Dictionary after deletion is : {'Akshat': 1, 'Akash': 2}
Example 3: Pop an element not present from the dictionary
The behavior of pop() function is different when the key is not present in dictionary. In this case, it returns the default provided value or KeyError in case even default is not provided.
Python3
# Python 3 code to demonstrate # working of pop() without key # initializing dictionary test_dict = { "Nikhil" : 7 , "Akshat" : 1 , "Akash" : 2 } # Printing initial dict print ( "The dictionary before deletion : " + str (test_dict)) # using pop to return and remove key-value pair # provided default pop_ele = test_dict.pop( 'Manjeet' , 4 ) # Printing the value associated to popped key # Prints 4 print ( "Value associated to popped key is : " + str (pop_ele)) # using pop to return and remove key-value pair # not provided default pop_ele = test_dict.pop( 'Manjeet' ) # Printing the value associated to popped key # KeyError print ( "Value associated to popped key is : " + str (pop_ele)) |
Output :
The dictionary before deletion : {'Nikhil': 7, 'Akshat': 1, 'Akash': 2}
Value associated to popped key is : 4
Traceback (most recent call last):
File "main.py", line 20, in
pop_ele = test_dict.pop('Manjeet')
KeyError: 'Manjeet'
We have discussed the use of dictionary pop() method in Python. It is very important function to remove values from a dictionary. Hope you understood the working of dictionary pop() in this article.
Also Read:
Python Dictionary pop() Method
Python dictionary pop() method removes and returns the specified element from the dictionary.
Example:
Python3
# inializing dictionary student student = { "rahul" : 7 , "Aditya" : 1 , "Shubham" : 4 } # priting original dictionary print (student) # using dictionary pop suspended = student.pop( "rahul" ) # checking key of the element print ( "suspended student roll no. = " + str (suspended)) # printing list after performing pop() print ( "remaining student" + str (student)) |
Output
{'rahul': 7, 'Aditya': 1, 'Shubham': 4} suspended student roll no. = 7 remaining student{'Aditya': 1, 'Shubham': 4}
Contact Us