Python Dictionary pop() Method

Removes the element with the specified key, and returns that value, or returns a default value if they key isn't found in the dictionary.

Syntax

Python
dictionary.pop(key)
dictionary.pop(key, default)

Parameters

ParameterDescription
key Required. The key of the value to return
default Optional. The value to be returned if the dictionary has no item keyed by key.

Example

Python
fish = {
    'guppy': 2,
    'zebra' : 5,
    'betta': 10
}

fish.pop('zebra')
print(f'My fish: {fish}')

Output

My fish: {'guppy': 2, 'betta': 10}

Notes

The pop() function will return the value of the item removed.

Example

Python
fish = {
    'guppy': 2,
    'zebra' : 5,
    'betta': 10
}

z = fish.pop('zebra')
print(f'I had {z} zebra fish')

Output

I had 5 zebra fish