Dict Methods
dict.pop()
Deletes an existing key and returns the associated object. An absent key returns the explicitly supplied fallback without inserting it; omitting the fallback raises KeyError. An explicit None fallback differs from an omitted fallback. The operation mutates the original dictionary, so aliases and views observe the removal. Use hashable keys. Hashing can raise TypeError for an unhashable key; local CPython can bypass hashing when the dictionary is empty, so pop on an empty dictionary with a default may return that default even for an unhashable argument. Do not rely on this implementation shortcut.
Formal Syntax & Structure
python syntaxpython
mapping.pop(key, /)mapping.pop(key, default, /)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| key | Hashable | Key to locate using hashing and equality. |
| default | object | Fallback for an absent key; if omitted, absence raises KeyError. |
Return Value
object
Removed value or explicit fallback.
Working Example
Example Snippetpython
queue = {"draft": 2, "review": 1}print(queue.pop("draft"))print(queue.pop("missing", 0))print(queue)