Syntax of Dictionary pop()
dict.pop(key, def)
Parameters :
- key : The key whose key-value pair has to be returned and removed.
- def : The default value to return if specified key is not present.
Returns :
- The value associated with the deleted key-value pair, if the key is present.
- Default value if specified if key is not present.
- KeyError, if key not present and default value not specified.
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}