In Python, the dictionary pop() method is used to remove an element from the dictionary. This method removes the element from the specified key in the given list.
If the particular key is present in the dictionary, it is removed from the dictionary, and the value is returned. If the specified key is not present, it raises a KeyError error.
The following is the syntax of the dictionary pop() method:
We will now look at some examples of the dictionary pop() method.
In this example, we will understand the working of the dictionary pop() method in Python.
Output:
Given Dictionary: {'apple': 20, 'banana': 14, 'watermelon': 2, 'kiwi': 12, 'oranges': 24}
Popped Value: 12
Updated Dictionary: {'apple': 20, 'banana': 14, 'watermelon': 2, 'oranges': 24}
Explanation:
In the above example, we are given a dictionary. We used the pop() method to remove the specified key from the dictionary. As a result, the key is removed and the value associated with that key is returned.
In case the specified key is not present in the dictionary, it raises a KeyError error. Here is an example:
Output:
Given Dictionary: {'apple': 20, 'banana': 14, 'watermelon': 2, 'kiwi': 12, 'oranges': 24}
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
/tmp/ipython-input-5-253658983.py in | ()
12
13 # using the pop() method to remove the specified key
---> 14 popped-value = fruits.pop('mangoes') # here key is not present
15 print("Popped Value:", popped-value)
16 print("Updated Dictionary:", fruits)
KeyError: 'mangoes'
| Explanation:
In this example, we are given a dictionary. We used the pop() method to remove a key that is not present in the dictionary. As a result, it raises a KeyError error.
In order to avoid the KeyError error when the key is not present, we can set a default value.
Let us see the following example to understand how it works.
Output:
Given Dictionary: {'apple': 20, 'banana': 14, 'watermelon': 2, 'kiwi': 12, 'oranges': 24}
Popped Value: 10
Updated Dictionary: {'apple': 20, 'banana': 14, 'watermelon': 2, 'kiwi': 12, 'oranges': 24}
Explanation:
Here, we are given a dictionary. We used the pop() method to remove the specified key. We have also mentioned the default value to be returned in case the specified key is not present in the dictionary. As a result, the default value is returned.
Let us take a look at another example of the dictionary pop() method to understand how the default value parameter works.
Output:
Given Dictionary: {'apple': 20, 'banana': 14, 'watermelon': 2, 'kiwi': 12, 'oranges': 24}
Popped Value: 12
Updated Dictionary: {'apple': 20, 'banana': 14, 'watermelon': 2, 'oranges': 24}
Explanation:
Here, we are given a dictionary. We used the pop() method to remove the specified key. We have also mentioned the default value to be returned in case the specified key is not present in the dictionary. Since the key exists in the dictionary, the returned value is the value associated with the specified key.
We request you to subscribe our newsletter for upcoming updates.