Python Dictionary pop() Method

Python Dictionary Pop() Method

This is a detailed tutorial of the Python Dictionary pop() Method. Learn to remove a particular item from a dictionary using this method with examples.

Python Dictionary pop()

The method dict.pop() is used to remove a particular element from a given dictionary by its key and this method also returns the removed item value.

Syntax

Given below is the syntax of the pop() method.

pop() Parameters

This method of the Python Dictionary can take up to two arguments and these arguments are described below.

  • key. This is the key of the item that you want to remove from a given dictionary.
  • default. This is the default value to be returned in case the key passed as the first argument of this method does not exist in the given dictionary.

pop() Return Value

The pop() method may return different values as per the different conditions specified below.

  • If the key passed in the first argument exists in the dictionary, the item value with that key is returned.
  • If the key passed in the first argument does not exist in the dictionary, then the default value specified in the second argument is returned.
  • If the key passed in the first argument does not exist in the dictionary and also the second argument value is Not passed, this method raises KeyError Exception.

Examples

The following three examples illustrate the different use-cases of the dict.pop() method.

Example 1. Removing the Item from the dictionary using pop(), if the key exists

In this example, we’re popping out a dictionary item with key "Papaya" and checking the value of the removed item key.


#A Dictionary of 5 items
items = {
"Brinjal (Eggplant)" : "Vegetable",
"Onion" : "Vegetable",
"Papaya" : "Fruit",
"Mango" : "Fruit",
"Radish" : "Vegetable"
}


#Popping the item with key Papaya
value = items.pop("Papaya")


print("items:", items)
print("Returned Value:", value)


The above program gives the following output.

Example 2. Working of pop() if the key does not exist

Here we’re trying to make use the method pop() to remove the item whose key actually does not exist in the given dictionary and here we’re not passing the second argument.


#A Dictionary of 5 items
items = {
"Brinjal (Eggplant)" : "Vegetable",
"Onion" : "Vegetable",
"Papaya" : "Fruit",
"Mango" : "Fruit",
"Radish" : "Vegetable"
}


#Popping the item with key Banana
#Item with this key not present in the dictionary
value = items.pop("Banana")


print("items:", items)
print("Returned Value:", value)


As the key passed in the first argument of this method is not having any associated item, this method raises the keyError.

Output.

Example 3. If the key does not exist in the dictionary but the default argument is passed

In this example, we’ve also passed the value for the second argument i.e. the default argument, so that if the key is not there in the dictionary, this method should not raise the keyError and should return the value specified in the second argument.


#A Dictionary of 5 items
items = {
"Brinjal (Eggplant)" : "Vegetable",
"Onion" : "Vegetable",
"Papaya" : "Fruit",
"Mango" : "Fruit",
"Radish" : "Vegetable"
}


#Popping the item with key Banana
#Item with this key not present in the dictionary
#Value of default is also passed
value = items.pop("Banana", "Fruit")


print("items:", items)
print("Returned Value:", value)


Output.

I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

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