Python dictionary pop
Python Dictionary Pop
In Python, a dictionary is a mutable container model that can store any number of Python objects, such as integers, floating-point numbers, strings, lists, tuples, and so on. Each of these is associated with a unique key, which serves as an index to access the object.
The Python dictionary pop() method is used to remove and return the value corresponding to the specified key.
Syntax
The syntax of the dictionary pop() method is as follows:
dict.pop(key[, default])
Here, key represents the key to be removed, and default is the default value to be returned (if the key does not exist).
Parameters
The parameters of the pop() method are described below:
- key: The key to be removed. If the key exists in the dictionary, its corresponding value is removed and returned; otherwise, a KeyError exception is raised (if the default parameter is not specified).
- default: If the key does not exist, the default value is returned (no exception is raised). If the default argument is not specified, it defaults to None.
Return Value
This method returns the deleted value (or default value). If the key does not exist and no default value is specified, a KeyError exception is raised.
Example Code
Here are several examples of the pop() method:
Example 1: Delete a specified key and its corresponding value
# Create a dictionary
fruits = {'apple': 10, 'banana': 20, 'orange': 30}
# Delete a specified key
returned_value = fruits.pop('apple')
print('Dictionary after deletion: ', fruits)
print('Returned value: ', returned_value)
Output:
Dictionary after deletion: {'banana': 20, 'orange': 30}
Returned value: 10
Example 2: Delete non-existent keys and return the default value
# Create a dictionary
fruits = {'apple': 10, 'banana': 20, 'orange': 30}
# Assign a non-existent key and return the default value
returned_value = fruits.pop('mango', 40)
print('Dictionary after deletion: ', fruits)
print('Returned value: ', returned_value)
# Create a dictionary
fruits = {'apple': 10, 'banana': 20, 'orange': 30}
# Assign a non-existent key and return the default value
returned_value = fruits.pop('mango', 40)
print('Dictionary after deletion: ', fruits)
print('Returned value: ', returned_value)
Output:
Dictionary after deletion: {'apple': 10, 'banana': 20, 'orange': 30}
Returned value: 40
Example 3: Delete non-existent keys without assigning a default value
</pre>
<p>Output:</p>
<pre><code class="language-python line-numbers">Traceback (most recent call last):
File "pop.py", line 6, in <module>
returned_value = fruits.pop('mango')
KeyError: 'mango'
</pre>
<p>Output:</p>
<pre><code class="language-python line-numbers">Traceback (most recent call last):
File "pop.py", line 6, in <module>
returned_value = fruits.pop('mango')
KeyError: 'mango'
In this example, because the specified key ‘mango’ does not exist in the dictionary, attempting to delete it using the pop() method without a default value results in a KeyError exception.
Conclusion
The Python dictionary pop() method can be used to remove and return a specified key and its corresponding value. It can also return a default value (if specified) and has different handling options for non-existent keys. When using this method, ensure that the specified key exists in the dictionary.