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}

Similar Reads

Definition and Use of Python Dictionary pop() Method

...

Syntax of Dictionary pop()

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....

How to Remove Value-Key Pair from Dictionary

dict.pop(key, def)...

More Example of Dictionary pop() method

To remove a value-key pair from a Python Dictionary you can use dictionary pop() method....

Contact Us