Definition and Use of Python Dictionary pop() Method
Dictionary pop() function in Python is an in-built function that is used to remove and return an element from a dictionary. It can take one or two arguments.
Dictionary pop() function is very useful in the conditional removal of elements and handling missing values. It not only allows us to remove an element but we can also access its value.
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