Python 3 reference · Dictionary Methods
Python Dictionary pop() Method
Remove a key and return its value. Returns the default if given and the key is missing; otherwise raises KeyError.
Python 3 Since 3.0
Syntax
dict.pop(key)
dict.pop(key, default)
Parameters
| Parameter | Description |
|---|---|
key |
The key to remove. |
default optional |
Returned if the key is missing. |
Return value
The removed value.
Examples
user = {"name": "Ada", "password": "secret"}
pw = user.pop("password")
print(user, pw)
Output
{'name': 'Ada'} secretopts = {"debug": True}
print(opts.pop("verbose", False), opts)
Output
False {'debug': True}Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
dict.popitem()· Remove and return the last inserted (key, value) pair (LIFO order). Raises KeyError if the dict is empty.dict.get()· Return the value for a key, or a default (None unless given) if the key is missing, without raising KeyError.dict.clear()· Remove every key and value from the dictionary in place, leaving it empty. Other references see the change too.list.pop()· Remove and return the item at a position, the last item by default. Raises IndexError on an empty list.- Python Dictionaries · tutorial