Dictionary method

dict.update()

Adds the keys and values from another dictionary (or from pairs or keyword arguments), replacing values for keys already there.

BeginnerEvery example below runs in your browser. Change the code and press Run.

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

update() copies entries into a dictionary. New keys are added; keys that already exist get the new value. The dictionary is changed in place.

It is the usual way to apply user settings on top of defaults, merge results from several sources, or set several keys in one call.

Remember the direction: in a.update(b), b wins when both have the same key. And update() returns None, so merged = a.update(b) gives you None. For a new merged dictionary, use a | b (Python 3.9 and later) or {**a, **b}.

Parameters

ParameterWhat it is
other
optional
A dictionary, or any collection of key-value pairs such as a list of 2-item tuples.
**kwargs
optional
Extra entries written as key=value. This only works when the keys are valid Python names, such as color="red".

Return value

Nothing (None). The dictionary is changed in place.

Examples

1Merge two dictionaries

Pythoneditable
fruit = {"apple": 3, "pear": 1}
fruit.update({"pear": 5, "kiwi": 2})
print(fruit)
Output
{'apple': 3, 'pear': 5, 'kiwi': 2}

"pear" keeps its place but gets the new value; "kiwi" is added at the end.

2User settings over defaults

Pythoneditable
defaults = {"theme": "light", "font": 14, "lang": "en"}
user = {"theme": "dark"}
settings = defaults.copy()
settings.update(user)
print(settings)
print(defaults)
Output
{'theme': 'dark', 'font': 14, 'lang': 'en'}
{'theme': 'light', 'font': 14, 'lang': 'en'}

Copying first keeps defaults unchanged for the next user.

3Keywords and pairs

Pythoneditable
car = {"brand": "Volta"}
car.update(color="red", seats=5)
car.update([("year", 2024), ("doors", 4)])
print(car)
Output
{'brand': 'Volta', 'color': 'red', 'seats': 5, 'year': 2024, 'doors': 4}

4A new dictionary with | instead

The | operator (Python 3.9+) builds a merged copy and leaves both dictionaries alone; |= updates in place like update().

Pythoneditable
base = {"tax": 0.2, "currency": "EUR"}
local = {"tax": 0.24}
merged = base | local
print(merged)
print(base)
Output
{'tax': 0.24, 'currency': 'EUR'}
{'tax': 0.2, 'currency': 'EUR'}

Common mistakes

Storing the result of update()

update() returns None. Call it on its own line, or use a | b when you want a value back.

Pythoneditable
a = {"x": 1}
result = a.update({"y": 2})
print(result)
print(a)
Output
None
{'x': 1, 'y': 2}

Expecting nested dictionaries to be merged

update() replaces the value of a key as a whole. A nested dictionary is not merged key by key; the new one simply takes its place.

Pythoneditable
config = {"db": {"host": "local", "port": 5432}}
config.update({"db": {"port": 6000}})
print(config)
Output
{'db': {'port': 6000}}

Questions people ask

How do I merge two dictionaries in Python?

In place: a.update(b). As a new dictionary: a | b (3.9+) or {**a, **b}. In all three, b wins on shared keys.

How do I add a single key to a dictionary?

Just assign it: d["key"] = value. update() is handiest for several keys at once.

How do I update only keys that are missing?

Reverse the merge: d = defaults | d, or call d.setdefault(k, v) for each default.

Spotted a mistake on this page? Tell us