Python Dictionary pop()
In the python dictionary, the pop() method removes a specific item from the dictionary and returns the value of the given item.
Python Dictionary pop()
The syntax of pop() is:
1
2
dictionary.pop(key, default)
pop() Parameters
The pop() method takes two parameters as arguments.
- key - name of the key that is to be removed.
- default (Optional) - A value that returns when a given key does not exist.
Let’s see some examples of the pop() method in the python dictionary.
Example 1: How to use the pop() method on dictionaries?
1
2
3
4
5
6
cars = { 1 : "BMW", 2 : "TOYOTA", 3 : "TATA"}
print("The popped item is :", cars.pop(2))
print("The new Dictionary is :",cars)
Output :
1
2
3
The popped item is : TOYOTA
The new Dictionary is : {1: 'BMW', 3: 'TATA'}
when we try to remove an item which is not present in the dictionary it will return an KeyError exception.
Example 2: Removing items that are not present in the dictionary?
1
2
3
4
5
6
cars = {"BMW" : 1,"TOYOTA" : 2,"TATA" : 3}
print("The popped item is :", cars.pop("AUDI"))
print("The new Dictionary is :",cars)
Output
1
2
3
4
5
Traceback (most recent call last):
File "", line 3, in <module>
print("The popped item is :", cars.pop("AUDI"))
KeyError: 'AUDI'
Rules of pop()
- If key is found - removed/popped element from the dictionary
- If key is not found - value specified as the second argument (default)
- If key is not found and default argument is not specified - KeyError exception is raised
This post is licensed under CC BY 4.0 by the author.