Methods and Functions

Python Dictionary pop()

In a dictionary that contains item values and their respective keys, we may sometimes want to remove elements once they are no longer required.

Python pop dictionary

 

To do this, we use the Python pop dictionary method with the following syntax:

                    

dictionary.pop(key, default)

pop() Parameters

The pop() method accepts the following two parameters:

1. Key: It is a required parameter and is the key name that you want to remove/pop from the dictionary.

2. Default: It is an optional parameter and defines the value that the interpreter should return if the specified key does not exist.

Return value from pop()

If the specified key exists in the dictionary, the Python pop dictionary method will return the value associated with the key. If the key does not exist, it will return the value of the default parameter that you specify. However, if the key is not found and the default value is not specified, the interpreter will throw a KeyError exception.

Popping an element from the dictionary

Let us look at an example of how to remove an element when the key is present.

                    

# removing element with pop()

# create dictionary
grades = { 'Science': 'A', 'Social': 'A', 'Math': 'B' }

elem = grades.pop('Social')
print ('Value of popped key is', elem)
print ('The dictionary now is', grades) 

Output:

                    

Value of popped key is A
The dictionary now is {'Science': 'A', 'Math': 'B'}

Popping an element not present from the Dictionary

Look at what happens if we try to pop an element that does not exist in the dictionary:

                    

# create dictionary
inventory = {'Shirts': 57, 'Pants': 65, 'Scarfs': 50}
elem = inventory.pop('Shoes')

Output:

                    

Traceback (most recent call last):
  File "", line 4, in 
KeyError: 'Shoes'

Popping an element not present from the dictionary provided a default value

                    

# inventory dictionary
inventory = {'Shirts': 57, 'Pants': 65, 'Scarfs': 50}

elem = inventory.pop('Shoes', 'Hats')

print ('Value of popped key is', elem)
print ('The dictionary now is', inventory)

Output:

                    

Value of popped key is Hats
The dictionary now is {'Shirts': 57, 'Pants': 65, 'Scarfs': 50}

As you can see, this does not have any effect on the existing dictionary.

Questions and Answers

Q1. Can you pop a dictionary in Python?

Yes, the Python pop dictionary method is an in-built function that allows you to remove/pop items from a dictionary. It removes items based on the key you specify in the parameters while returning the value of that key.

Q2. How do you pop a dictionary?

You can pop elements from a dictionary through the pop() method if you follow this syntax:

                    

dictionary.pop(key, default)

In the method parameters, you have to specify the key name of the element you want to remove. You can also set an optional parameter (default) that the interpreter will return if the key is not in the dictionary.

Q3. What does pop() do in Python?

When you use the pop() method in Python, it searches for the key you have specified in the parameter. If the key exists in the dictionary, the element gets deleted, and its associated value is returned.

If you try to use the pop() method for a key that does not exist in the dictionary, the default value gets returned (if specified), or the interpreter raises a KeyError exception.

Q4. How is pop () different from Popitem ()?

Although we use pop() and popitem() to remove elements from a dictionary, they are actually different. pop() can remove any item from a dictionary as long as you specify the key.

On the other hand, popitem() can only remove and return the value of the last element in the dictionary.

For example:

                    

# create dictionary
grades = { 'Science': 'A', 'Social': 'A', 'Math': 'B', 'English': 'A' }

# using pop()
elem = grades.pop('Social')
print ('Value of popped key is', elem)
print ('The dictionary now is', grades) 

# using popitem()
elem = grades.popitem()
print ('Value returned', elem)
print ('The dictionary now is', grades)

Output:

                    

Value of popped key is A
The dictionary now is {'Science': 'A', 'Math': 'B', 'English': 'A'}
Value returned ('English', 'A')
The dictionary now is {'Science': 'A', 'Math': 'B'}

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.