Chapters
Python114 chapters

ReferenceDictionary methods

dict.pop()

Removes a key and returns its value.

Signature

mapping.pop(key, default=...)

Parameters

NameMeans
keywhat to remove
defaultreturned instead of raising when it is missing

Returns

The value. Raises KeyError with no default.

Example

Python
person = {"name": "Ada", "tmp": 1}
print(person.pop("tmp"))
print(person)
print(person.pop("missing", "not there"))

Output

1
{'name': 'Ada'}
not there

See also