mutation

dict.update

Updates the dictionary in place with key/value pairs from other, overwriting existing keys. Accepts another mapping, an iterable of key/value pairs, and/or keyword arguments; returns None.

d.update([other])

Parameters

Parameter Purpose
other (mapping) Another dict whose entries are merged in
other (iterable) Iterable of (key, value) pairs, e.g. list of 2-tuples
**kwargs Keyword arguments become key=value entries

Examples

>>> d = {'a': 1}
>>> d.update({'b': 2, 'a': 99})
>>> d
{'a': 99, 'b': 2}

Existing keys are overwritten

>>> d = {}
>>> d.update([('x', 1), ('y', 2)])
>>> d
{'x': 1, 'y': 2}

Accepts an iterable of pairs

>>> d = {'a': 1}
>>> d.update(b=2, c=3)
>>> d
{'a': 1, 'b': 2, 'c': 3}

Keyword arguments work too (keys must be valid identifiers)

>>> d = {'a': 1}
>>> print(d.update({'b': 2}))
None

update returns None — it mutates in place

Gotcha

update returns None, not the dict — do not chain (e.g. d.update(...).items() fails). Later sources win: kwargs override the positional mapping.

Related methods

← All Python dict methods · String methods · Built-in functions