Python Glossary Русский

all Python 3Dictionaries (dict)function

dict.pop

removal

Remove a key and return its value; default or KeyError if the key is absent.

Syntax

d.pop(key[, default])

Do not confuse it with list.pop(), whose argument is an index and which has no default parameter; here the first argument is a key. d.pop(key, None) is the usual way to drop a key that may not be there — del d[key] would raise KeyError instead. Because it returns the value, pop is handy when you need to take and remove in one step, such as pulling a service key out of a dict of options.

Examples

d = {'a': 1, 'b': 2}
print(d.pop('a'))       # → 1
print(d.pop('z', -1))   # → -1
print(d)                # → {'b': 2}

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy