The pop()
method in Python is a built-in method that is used to remove and return an item from a dictionary. The method removes the key-value pair specified by the key and returns the value associated with the key. If the key does not exist, a KeyError
is raised.
Syntax
dict.pop(key, default)
Parameters
The pop()
method takes the following parameters:
key
: The key of the key-value pair to remove from the dictionary. This parameter is required.default
: The value to return if the key is not found in the dictionary. This parameter is optional.
Examples
Example 1: Removing a Key-Value Pair from a Dictionary
Consider the following example, where we remove a key-value pair from a dictionary:
student = {'name': 'John', 'age': 25, 'gender': 'Male'} age = student.pop('age') print(student) print(age)
In this example, we have created a dictionary called student
, which contains the key-value pairs of a student. Then, we use the pop()
method to remove the key-value pair associated with the key ‘age’ from the dictionary. Finally, we print the dictionary and the value that was returned by the pop()
method, which will be:
{'name': 'John', 'gender': 'Male'} 25
As you can see, the key-value pair associated with the key ‘age’ has been removed from the dictionary and the value associated with the key has been returned and stored in the variable age
.
Example 2: Using the default Parameter with the pop() Method
Consider the following example, where we use the default
parameter with the pop()
method:
student = {'name': 'John', 'age': 25, 'gender': 'Male'} country = student.pop('country', 'Not Specified') print(student) print(country)
In this example, we have created a dictionary called student
, which contains the key-value pairs of a student. Then, we use the pop()
method to try to remove the key-value pair associated with the key ‘country’ from the dictionary. However, the key ‘country’ does not exist in the dictionary, so a KeyError
would normally be raised. To prevent this, we have specified the default value ‘Not Specified’ as the second parameter of the pop()
method. Finally, we print the dictionary and the value that was returned by the pop()
method, which will be:
{'name': 'John', 'age': 25, 'gender': 'Male'} Not Specified
As you can see, the key-value pair associated with the key ‘country’ was not found in the dictionary, so the default value ‘Not Specified’ was returned and stored in the variable country
instead of raising a KeyError
.
Example 3: Removing the Last Key-Value Pair from a Dictionary
Consider the following example, where we remove the last key-value pair from a dictionary:
student = {'name': 'John', 'age': 25, 'gender': 'Male'} last_item = student.popitem() print(student) print(last_item)
In this example, we have created a dictionary called student
, which contains the key-value pairs of a student. Then, we use the popitem()
method to remove the last key-value pair from the dictionary. Finally, we print the dictionary and the value that was returned by the popitem()
method, which will be:
{'name': 'John', 'age': 25} ('gender', 'Male')
As you can see, the last key-value pair has been removed from the dictionary and the value has been returned as a tuple, where the first item is the key and the second item is the value.
In conclusion, the pop()
method in Python is a useful method that allows you to remove and return a key-value pair from a dictionary. It is important to note that the method raises a KeyError
if the key specified as the parameter does not exist in the dictionary. By using the default parameter, you can prevent the KeyError
from being raised and instead return a default value if the key is not found in the dictionary.