Python Glossary Русский

all Python 3Dictionaries (dict)construct

del d[key]

access

The del operator removes a pair by key (KeyError if absent). To delete and return a value, use dict.pop()/dict.popitem().

Syntax

del d[key]

del returns nothing — if you still need the removed value use d.pop(key), and d.pop(key, None) to avoid KeyError on a missing key. Never delete keys inside for k in d: changing the dictionary's size during iteration raises RuntimeError, so collect the keys to drop first (list(d) or a comprehension) and delete afterwards.

Examples

d = {'a': 1, 'b': 2}
del d['a']
print(d)          # → {'b': 2}
print('a' in d)   # → False

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy