Python language specified pop() for almost all containers, be it list, set etc. This particular article focuses on illustrating the pop() method offered by Python dictionaries. This method is useful for the programmers who deal with the dictionary too often.
Syntax : 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 :
Value associated to deleted key-value pair, if key is present.
Default value if specified if key is not present.
KeyError, if key not present and default value not specified.
Code #1 : Demonstrating working of pop(), when key is present.
# 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 poppped 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 poppped key is : 2
Dictionary after deletion is : {'Nikhil': 7, 'Akshat': 1}
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.
Code #2 : Demonstrating the working of pop() without key
# 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 poppped 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 poppped key is : " + str(pop_ele)) |
Output :
The dictionary before deletion : {'Nikhil': 7, 'Akshat': 1, 'Akash': 2}
Value associated to poppped key is : 4
Traceback (most recent call last):
File "main.py", line 20, in
pop_ele = test_dict.pop('Manjeet')
KeyError: 'Manjeet'
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert nested dictionary into flattened dictionary
- Python | Convert string dictionary to dictionary
- Python | Pretty Print a dictionary with dictionary value
- Regular Dictionary vs Ordered Dictionary in Python
- Python | Dictionary initialization with common dictionary
- Python - Update dictionary with other dictionary
- Python - Filter dictionary values in heterogenous dictionary
- Python - Convert Dictionary Value list to Dictionary List
- Python - Replace dictionary value from other dictionary
- Python - Append Dictionary Keys and Values ( In order ) in dictionary
- Python - Combine two dictionaries having key of the first dictionary and value of the second dictionary
- Python program to update a dictionary with the values from a dictionary list
- List Methods in Python | Set 2 (del, remove(), sort(), insert(), pop(), extend()...)
- Python Set | pop()
- Python list | pop()
- Python | Pandas Dataframe.pop()
- Python | Pandas Series.pop()
- Wand push() and pop() in Python
- PyQt5 - Show pop up items of ComboBox when push button is pressed
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

