Python Dictionary pop()

Python dictionary pop() method

Python dictionary pop() method removes the item with specified key in the dictionary, and returns the corresponding value of the removed item.

In this tutorial, you will learn about dictionary pop() method in Python, and its usage, with the help of example programs.

Syntax of dict pop()

The syntax of dictionary pop() method is

dictionary.pop(key, default_value)

Parameters

ParameterDescription
keyRequired
The key which has to be removed from the dictionary.
default_valueOptional
If specified key does not exist, pop() returns this value.
Python dictionary pop() method parameters

Return value

  • If the specified key is present, pop() removes the key (modifies the original dictionary) and returns the corresponding value.
  • If the specified key is not present, and default value (second argument) is given, then pop() returns this default value.
  • If the key is not present, and the default value (second argument) is not given, then pop() raises KeyError.

Examples for pop() method

1. Removing a key from dictionary using pop() method in Python

In this example, we are given a dictionary in my_dict with some initial key:value pairs. We have to remove the key corresponding to the key ‘bar’.

Call pop() method on the given dictionary object my_dict, and pass the key ‘bar’ as argument. We shall print the value of this removed key, and also print the modified dictionary.

Python Program

my_dict = {
    'foo':12,
    'bar':14,
    'moo':16
}

removed_value = my_dict.pop('bar')

print(f"Removed value : {removed_value}")
print(my_dict)
Run Code Copy

Since the key ‘bar’ is present in the dictionary, pop() method removes the item and returns the value 14 corresponding to that key.

Output

Removed value : 14
{'foo': 12, 'moo': 16}

2. Dictionary pop() – Key not present, and default value given

In this example, we try to pop the key ‘poo’ from the dictionary, but the key is not present in the dictionary. We also passed a default value of 0 for second argument.

Python Program

my_dict = {
    'foo':12,
    'bar':14,
    'moo':16
}

removed_value = my_dict.pop('poo', 0)

print(f"Removed value : {removed_value}")
print(my_dict)
Run Code Copy

Since the key ‘poo’ is not present in the dictionary, and the default value (second argument) is specified, pop() method returns the default value. Also, since we did not remove any key, the original dictionary remains same as that of original.

Output

Removed value : 0
{'foo': 12, 'bar': 14, 'moo': 16}

3. Dictionary pop() – Key not present, default value not given

In this example, we try to pop the key ‘poo’ from the dictionary, but the key is not present in the dictionary. We also did not pass a default value for second argument.

Python Program

my_dict = {
    'foo':12,
    'bar':14,
    'moo':16
}

removed_value = my_dict.pop('poo')

print(f"Removed value : {removed_value}")
print(my_dict)
Run Code Copy

Since the key ‘poo’ is not present in the dictionary, and the default value is not specified, pop() method raises KeyError.

Output

Traceback (most recent call last):
  File "/Users/pythonexamplesorg/main.py", line 7, in <module>
    removed_value = my_dict.pop('poo')
                    ^^^^^^^^^^^^^^^^^^
KeyError: 'poo'

Summary

In this tutorial of Python Dictionary Methods, we learned how to use Dictionary pop() method to remove a specified key from the dictionary, with help of well detailed Python programs.

Related Tutorials

Code copied to clipboard successfully 👍