The pop() method of Python dictionary (dict) is used to remove the element from the dictionary by dict key
and return the value
related to the removed key
. If a key does not exist in the dictionary and the default value is specified, then returns the default value; else throws a KeyError
.
In this article, I will explain about Python dictionary pop()
method with examples which includes removing the specified key:value
pair and returns a value related to the removed key:value
pair.
All dictionary methods are available on the dictionary methods page.
1. Quick Examples of Dictionary pop() Method
If you are in a hurry, below are some quick examples of the Python dictionary pop() method.
# Quick examples of dictionary pop() method
# Example 1: Delete the key element using pop() method
technology = { 'course':'python', 'fee': 4000, 'duration': '60 days' }
item=technology.pop('fee')
# Example 2: Return the default value 'Richard'
technology = { 'course':'python', 'fee': 4000, 'duration': '60 days' }
item=technology.pop('tutor','Richard')
# Example 3: Get the keyerror
technology = { 'course':'python', 'fee': 4000, 'duration': '60 days' }
item=technology.pop('tutor')
2. Syntax of pop()
Following is the syntax of the Python dictionary pop() method.
# Syntax of the pop()
dict.pop(key,default)
2.2 Parameters
pop() method allows two parameters:
key
: (required) The key to be removed or searched from the dic.default
: (optional) If provided, this value will be returned if the key is not found in the dictionary. If not provided and the key is not found, aKeyError
is raised.
2.3 Return value
The dictionary pop()
method returns:
- Returns the
value
related to the removed key: value pair, if thekey
is present in the dictionary. - Returns the
default value
if the default parameter is specified and thekey
is not present in the dictionary. - Returns
KeyError
if the default parameter is not specified and thekey
is not present in the dictionary.
3. Usage of Python Dictionary pop() Method
The python pop()
method removes an element from the dictionary. It returns the element which is related to the removed key. If the key is present in the dictionary, it removes and returns its value. If the key is not present in the dictionary and the default value is specified, then it returns the default value. Else throws a KeyError
.
3.1 Pop Element From Dictionary
To pop an element from a dictionary in Python, you can use the pop()
method. For instance, the pop('fee')
method removes the key-value pair with the key ‘fee’ from the dictionary, and the removed value (4000 in this case) is stored in the variable item
.
# Delete the key element using pop() method
technology = { 'course':'python', 'fee': 4000, 'duration': '60 days' }
item=technology.pop('fee')
print("Removed value:",item)
print("Updated dictionary:",technology)
Yields below output.
3.2 Pop Element Not Present In Dictionary
If a key
element is not present in the dictionary and the default value
is provided, it returns the specified default value. For example, since the key ‘tutor’ is not present in the dictionary, the pop()
method returns the default value (‘Richard’), and the dictionary remains unchanged.
# Get the default value
technology = { 'course':'python', 'fee': 4000, 'duration': '60 days' }
item=technology.pop('tutor','Richard')
print("Popped value is:",item)
print("Updated dictionary:",technology)
# Outputs:
# Popped value is: Richard
# Updated dictionary: {'course': 'python', 'fee': 4000, 'duration': '60 days'}
3. 3 Default Param is Not Provided
When a key
element is not present in the dictionary and the default value
is not provided, it returns a KeyError
. For example,
# Get the keyerror
technology = {'course':'python','fee':4000,'duration':'60 days'}
item=technology.pop('tutor')
print("Popped value is:", item)
print("Updated dictionary:", technology)
# Outputs:
KeyError: 'tutor'
Frequently Asked Questions on Python Dictionary pop() Method
The pop()
method is used to remove and return the value associated with a specified key from a dictionary. If the key is not found, it can optionally return a default value or raise a KeyError
.
To use pop()
to remove an item from a dictionary, you need to provide the key of the item you want to remove.
If the key is not found and you haven’t provided a default value, a KeyError
will be raised. To handle this, you can either provide a default value or check for the key’s existence before using pop()
.
The pop()
method not only returns the value associated with the key but also removes the key-value pair from the dictionary, modifying it in the process.
The order of elements in a dictionary is not guaranteed. The pop()
method removes a specific key-value pair, but the order of the remaining elements may not be the same as the original order of insertion.
You can provide a default value as the second argument to pop()
to avoid a KeyError
if the key is not found.
Conclusion
In this article, I have explained about Python dictionary pop()
method with examples. By using, the pop()
method we can remove a specified key element in the dictionary at the same time, and it returns the corresponding value of the key. Moreover, I have explained how the behavior of the pop()
method whether the specified value is present in the dictionary or not.
Happy Learning !!
Related Articles
- get() Method of Python Dictionary
- clear() Method of Python Dictionary
- items() Method of Python Dictionary
- fromkeys() Method of Python Dictionary
- keys() Method of Python Dictionary
- values() Method of Python Dictionary
- copy() Method of Python Dictionary
- Copy the Dictionary and edit it in Python
- update() Method of Python Dictionary
- setdefault() Method of Python Dictionary
- popitem() Method of Python Dictionary
- Convert Two Lists into Dictionary in Python
- Iterate Python Dictionary using enumerate() Function