Dictionary pop() Method in Python

Python Dictionary pop() Method

The pop() method in Python dictionaries is used to remove an item from the dictionary based on the specified key. It returns the value associated with the removed key. If the specified key is not found in the dictionary, the pop() method raises a KeyError unless a default value is provided as the second argument. If a default value is provided and the key is not found, the pop() method returns the default value instead of raising an error.

Syntax of the pop() method:

value = dictionary.pop(key, default)
  • key: The key of the item that you want to remove from the dictionary.
  • default: (Optional) The default value to be returned if the specified key is not found in the dictionary. If not provided, it raises a KeyError when the key is not found.

Now, let's see some examples of how to use the pop() method:

Example 1: Removing an item from the dictionary

# Sample dictionary
fruit_colors = {'apple': 'red', 'banana': 'yellow', 'orange': 'orange'}

# Remove the item with key 'banana'
removed_color = fruit_colors.pop('banana')

# Print the removed color and the updated dictionary
print("Removed Color:", removed_color)
print("Updated Dictionary:", fruit_colors)

Output:

Removed Color: yellow
Updated Dictionary: {'apple': 'red', 'orange': 'orange'}

In this example, the pop() method is used to remove the key-value pair with the key 'banana' from the fruit_colors dictionary. It returns the value 'yellow', which was associated with the key 'banana'.

Example 2: Providing a default value when the key is not found

# Sample dictionary
fruit_colors = {'apple': 'red', 'banana': 'yellow', 'orange': 'orange'}

# Try to remove the item with key 'grape'
# As 'grape' key is not present, it will return the default value 'not found'
removed_color = fruit_colors.pop('grape', 'not found')

# Print the removed color and the updated dictionary
print("Removed Color:", removed_color)
print("Updated Dictionary:", fruit_colors)

Output:

Removed Color: not found
Updated Dictionary: {'apple': 'red', 'banana': 'yellow', 'orange': 'orange'}

In this example, the pop() method attempts to remove the key 'grape', which is not present in the fruit_colors dictionary. Since a default value 'not found' is provided, it returns 'not found'.

Example 3: Handling the KeyError when the key is not found (without default value)

# Sample dictionary
fruit_colors = {'apple': 'red', 'banana': 'yellow', 'orange': 'orange'}

try:
    # Try to remove the item with key 'grape'
    # As 'grape' key is not present, it will raise a KeyError
    removed_color = fruit_colors.pop('grape')
except KeyError as e:
    print(f"Error: {e}")

Output:

Error: 'grape'

In this example, the pop() method attempts to remove the key 'grape', which is not present in the fruit_colors dictionary. Since no default value is provided, it raises a KeyError with the message 'grape'. To handle this situation gracefully, we use a try-except block to catch the error and print a custom error message.

The pop() method is handy when you need to remove and retrieve items from the dictionary based on their keys.