Python Dictionary update() Method

Short answer
dict.update(other) copies every key-value pair from other into the dictionary, overwriting keys that already exist, changes the dictionary in place and returns None.

update() is the built-in way to merge data into an existing dictionary: applying user settings over defaults, adding new records, or changing several keys in one call. It is one of the core dictionary methods.

Because it modifies the dictionary you call it on, anything else referring to that same dictionary sees the change. If you want a merged new dictionary and keep the originals untouched, read how to merge two dictionaries.

Quick facts
Syntaxd.update(other), d.update(key=value, ...) or d.update([(k, v), ...])
ParametersA dictionary, an iterable of (key, value) pairs, and/or keyword arguments
ReturnsNone (the dictionary is changed in place)
Existing keysTheir values are overwritten by the new ones
Time complexityO(m), where m is the number of pairs being added

How dict.update() merges and overwrites keys

For every pair in the argument, update() does what d[key] = value does: it adds the key if it is new and replaces the value if the key already exists. Keys that are not mentioned are left alone. When the same key appears in several sources, the last one wins.

Python
settings = {"theme": "light", "font": 12}
settings.update({"font": 16, "lang": "en"})
print(settings)

Here "font" was overwritten, "lang" was added and "theme" stayed as it was.

Three ways to pass data to update()

You can pass a dictionary, an iterable of two-item pairs (a list of tuples, or the result of zip()), or keyword arguments. Keyword arguments only work when the keys are valid identifiers written as strings, such as d.update(age=21). You can combine a positional argument with keywords in one call; the keywords are applied last.

Python
d = {}
d.update([("a", 1), ("b", 2)])
d.update(c=3)
d.update(zip(["d", "e"], [4, 5]))
print(d)

update() vs the | operator and ** unpacking

Since Python 3.9 you can also merge with | and |=. The in-place form d |= other behaves like d.update(other), while a | b creates a new dictionary. Unpacking, {**a, **b}, also creates a new one. Choose update() when you intend to modify an existing dictionary, and the others when you want to leave the originals alone.

All four let the right-hand dictionary win.
Way to mergeChanges original?Result when keys clash
a.update(b)Yes, aValue from b wins
a |= bYes, aValue from b wins
a | bNo, new dictValue from b wins
{**a, **b}No, new dictValue from b wins

Examples you can run

Press Run, then edit the code and run it again.

Apply user settings over defaults

Copy the defaults first, then update the copy so the defaults stay intact.

Apply user settings over defaults
defaults = {"theme": "light", "font": 12, "lang": "en"}
user = {"theme": "dark"}
config = defaults.copy()
config.update(user)
print(config)
print(defaults)
Expected output{'theme': 'dark', 'font': 12, 'lang': 'en'}
{'theme': 'light', 'font': 12, 'lang': 'en'}

Update several prices at once

One call changes two existing prices and adds a new product.

Update several prices at once
prices = {"pen": 3, "book": 12}
prices.update({"pen": 4, "book": 15, "bag": 30})
print(prices)
Expected output{'pen': 4, 'book': 15, 'bag': 30}

update() returns None

Printing the call itself shows None; the changed dictionary is the original one.

update() returns None
d = {"a": 1}
result = d.update({"b": 2})
print(result)
print(d)
Expected outputNone
{'a': 1, 'b': 2}

Common mistakes

Assigning the result of update()

✗ Wrong

base = {"a": 1}
base = base.update({"b": 2})
print(base)
None

✓ Right

base = {"a": 1}
base.update({"b": 2})
print(base)

update() returns None. Call it on its own line and keep using the original dictionary.

Passing a plain list of keys

✗ Wrong

d = {}
d.update(["a", "b"])
ValueError: dictionary update sequence element #0 has length 1; 2 is required

✓ Right

d = {}
d.update([("a", 1), ("b", 2)])
print(d)

Each element must be a (key, value) pair. To give several keys the same value use fromkeys().

Expecting a copy to stay unchanged

✗ Wrong

a = {"x": 1}
b = a
b.update({"x": 2})
print(a)
{'x': 2} (a and b are the same dictionary)

✓ Right

a = {"x": 1}
b = a.copy()
b.update({"x": 2})
print(a)

b = a does not copy. Use a.copy() first; see dict.copy().

Python Dictionary update() Method: questions people ask

What does dict.update() do in Python?

It merges key-value pairs from another dictionary, iterable of pairs or keyword arguments into the dictionary, overwriting values for existing keys. It returns None.

How do I merge two dictionaries in Python?

Use a.update(b) to change a in place, or a | b (Python 3.9+) or {**a, **b} to make a new dictionary. See merging two dictionaries.

Does update() overwrite existing keys?

Yes. If a key exists in both, the value from the argument replaces the old value.

Keep learning

Step-by-step lessons:
Python DictionariesPython Dictionary Methods

Related:
How to Merge Two Dictionaries in PythonPython Dictionary copy() MethodPython Dictionary setdefault() MethodPython Dictionary fromkeys() Method

More in dictionary methods:
Python Dictionary get() MethodPython Dictionary items() MethodPython Dictionary keys() MethodPython Dictionary values() MethodPython Dictionary pop() MethodPython Dictionary popitem() MethodPython Dictionary setdefault() MethodPython Dictionary clear() Method See all →