Python Dictionary pop() method

01 Jul, 2022
Python dictionary pop() method removes and returns the specified element from the dictionary.
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. 
Python3
# Python 3 code to demonstrate# working of pop() # initializing dictionarytest_dict = {"Nikhil": 7, "Akshat": 1, "Akash": 2} # Printing initial dictprint("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 keyprint("Value associated to popped key is : " + str(pop_ele)) # Printing dictionary after deletionprint("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}
Python3
# Python 3 code to demonstrate# working of pop() # initializing dictionarytest_dict = {"Nikhil": 7, "Akshat": 1, "Akash": 2} # Printing initial dictprint("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 keyprint("Value associated to popped key is : " + str(pop_first)) # Printing dictionary after deletionprint("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}
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 dictionarytest_dict = {"Nikhil": 7, "Akshat": 1, "Akash": 2} # Printing initial dictprint("The dictionary before deletion : " + str(test_dict)) # using pop to return and remove key-value pair# provided defaultpop_ele = test_dict.pop('Manjeet', 4) # Printing the value associated to popped key# Prints 4print("Value associated to popped key is : " + str(pop_ele)) # using pop to return and remove key-value pair# not provided defaultpop_ele = test_dict.pop('Manjeet') # Printing the value associated to popped key# KeyErrorprint("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'
kumar_satyam
vinayedula
rkbhola5
python-dict
Python-dict-functions
Python
python-dict
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Taking input in Python
Read a file line by line in Python
Python String | replace()