Python dictionary pop() method removes and returns the specified element from the dictionary.
Example:
Python3
student = { "rahul" : 7 , "Aditya" : 1 , "Shubham" : 4 }
print (student)
suspended = student.pop( "rahul" )
print ( "suspended student roll no. = " + str (suspended))
print ( "remaining student" + str (student))
|
Output
{'rahul': 7, 'Aditya': 1, 'Shubham': 4}
suspended student roll no. = 7
remaining student{'Aditya': 1, 'Shubham': 4}
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.
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.
How to Remove Value-Key Pair from Dictionary
To remove a value-key pair from a Python Dictionary you can use dictionary pop() method.
Example
Python3
race_rank = { "Rahul" : 8 , "Aditya" : 1 , "Madhur" : 5 }
race_rank.pop( "Rahul" )
print (race_rank)
|
Output
{'Aditya': 1, 'Madhur': 5}
More Example of Dictionary pop() method
Let’s practice some of the use-case scenarios of python dictionary pop() method with some examples:
Example 1: Pop an element from the dictionary
Python3
test_dict = { "Nikhil" : 7 , "Akshat" : 1 , "Akash" : 2 }
print ( "The dictionary before deletion : " + str (test_dict))
pop_ele = test_dict.pop( 'Akash' )
print ( "Value associated to popped key is : " + str (pop_ele))
print ( "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}
Example 2: Python Dictionary pop() first element
Python3
test_dict = { "Nikhil" : 7 , "Akshat" : 1 , "Akash" : 2 }
print ( "The dictionary before deletion : " + str (test_dict))
pop_first = test_dict.pop( "Nikhil" )
print ( "Value associated to popped key is : " + str (pop_first))
print ( "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}
Example 3: Pop an element not present from the dictionary
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
test_dict = { "Nikhil" : 7 , "Akshat" : 1 , "Akash" : 2 }
print ( "The dictionary before deletion : " + str (test_dict))
pop_ele = test_dict.pop( 'Manjeet' , 4 )
print ( "Value associated to popped key is : " + str (pop_ele))
pop_ele = test_dict.pop( 'Manjeet' )
print ( "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'
We have discussed the use of dictionary pop() method in Python. It is very important function to remove values from a dictionary. Hope you understood the working of dictionary pop() in this article.
Also Read:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
28 Nov, 2023
Like Article
Save Article