Python Glossary Русский

all Python 3Dictionaries (dict)function

dict.update

modification

Add or replace pairs from another dict, an iterable of pairs, or keyword arguments (in place).

Syntax

d.update([other], **kwargs)

update() mutates the dict in place and returns None, so d = d.update(other) silently turns d into None — a classic beginner typo. Colliding keys are overwritten without warning and the old values are gone; if you want a new dict rather than a mutated original, use d1 | d2 (Python 3.9+). The argument need not be a dict either: any iterable of key/value pairs works, such as a list of tuples or a zip object.

Examples

d = {'a': 1}
d.update({'b': 2})
d.update(c=3)
print(d)        # → {'a': 1, 'b': 2, 'c': 3}
d.update(a=10)
print(d['a'])   # → 10 (перезапись)

See also

Open in the full glossarydocs.python.orgReport an inaccuracy