Python Dictionary update(): How to Use It
In this article
The update() method merges another dictionary or an iterable of key-value pairs into an existing dictionary, overwriting values for keys that already exist and adding keys that do not. Nothing needs to be installed first: it is a built-in method on every Python dictionary, available in Python 3 and in Python 2, and it changes the dictionary in place and returns None. The shortest working pattern is d.update(other), where other is a dict, a mapping, or a sequence of two-item pairs.
What You'll Need
- A working Python installation (any recent Python 3 release is fine; the method has existed for many versions).
- A way to run Python code: the interactive interpreter, a script file, or a notebook. The interactive interpreter is the quickest way to try examples.
- A dictionary you want to modify, and a second source of keys and values (a dict, a list of tuples, or keyword arguments).
Step-by-Step
- Create the target dictionary. For example,
settings = {"volume": 5, "brightness": 70}. This is the dictionary that will be changed. - Prepare the source of new data. It can be a plain dict such as
changes = {"volume": 8, "theme": "dark"}, a list of pairs such as[("volume", 8), ("theme", "dark")], or keyword arguments passed directly to the method. - Call
update()on the target. Writesettings.update(changes). The method reads the source and writes each key and value intosettings. - Understand the overwrite rule. Keys that exist in both dictionaries take the value from the source. So
volumebecomes8. Keys only in the source, liketheme, are added. Keys only in the target are left untouched. - Do not assign the result.
update()returnsNone. Writingsettings = settings.update(changes)replaces your dictionary withNone. Call the method on its own line and keep using the original variable. - Pass keyword arguments if the keys are valid identifiers.
settings.update(volume=8, theme="dark")works and is readable, but keyword keys must be strings that are valid Python names, so keys with spaces or starting with digits cannot be passed this way. - Use a list of pairs when you build data dynamically.
settings.update([("volume", 8), ("theme", "dark")])is useful when keys and values come from a loop or from reading a file. - Check the result. Print the dictionary or inspect it in the interpreter. You should see the merged contents, with source values winning on conflicts.
- Decide whether you need a copy. If you want the merged result without changing the original, build a new dictionary instead, for example
merged = {**settings, **changes}ormerged = settings.copy(); merged.update(changes). - Watch for nested structures.
update()is shallow. If both dictionaries have a key whose value is itself a dictionary, the source's inner dictionary replaces the target's inner dictionary entirely; it is not merged key by key. Merge nested dictionaries manually if you need that behaviour.
Common Mistakes
- Assigning the return value. The most frequent error is
d = d.update(x), which leavesdasNoneand causes aTypeErroron the next method call. - Expecting the original to stay unchanged.
update()mutates in place. If another variable points to the same dictionary, it sees the change too. - Passing a single pair instead of a sequence of pairs.
d.update(("a", 1))fails because each element of the outer sequence must itself be a two-item pair. Used.update([("a", 1)])ord.update(a=1). - Assuming deep merging. Nested dictionaries are replaced, not combined.
- Using unhashable keys. Keys must be hashable; lists and other dictionaries cannot be keys, whether you add them with
update()or by assignment. - Forgetting that later sources win. When you pass multiple sources or mix a mapping with keyword arguments, later values overwrite earlier ones for the same key.
Tips
- Use
update()when you want to change an existing dictionary; use{**a, **b}ora | b(Python 3.9 and later) when you want a new merged dictionary and no mutation. - Keep the source separate from the target so the intent is clear, and name variables after their role, such as
defaultsanduser_settings. - For configuration-style merges, copy the defaults first with
dict(defaults)so the shared defaults are never modified by accident. - If you need to merge only missing keys, loop over the source and use
setdefault()instead ofupdate(). - Remember that insertion order is preserved: new keys appear in the order they are added, and existing keys keep their original position.
Troubleshooting
TypeError: 'NoneType' object is not subscriptable: you assigned the result ofupdate(). Remove the assignment and call the method on its own line.ValueError: dictionary update sequence element #0 has length 1; 2 is required: one item in your sequence is not a two-element pair. Check the data you are passing and wrap single pairs in a list.TypeError: cannot convert dictionary update sequence element #0 to a sequence: you passed something that is neither a mapping nor a sequence of pairs, such as a plain string or a number. Convert it to a dict or a list of tuples first.- Unexpected values after the call: check the order of sources and whether keyword arguments were used; the last value supplied for a key wins.
- Original dictionary changed when you did not expect it: this is normal in-place behaviour. Make a copy before updating if the original must be preserved.
- Nested data disappeared: the merge is shallow. Merge inner dictionaries explicitly, one level at a time.
Frequently Asked Questions
Does update() return a new dictionary?
No. It modifies the dictionary in place and returns None. Use {**a, **b} or the | operator if you want a new dictionary.
What happens to keys that exist in both dictionaries?
The value from the source passed to update() replaces the existing value. Keys only in the target are kept; keys only in the source are added.
Can I pass a list of tuples to update()?
Yes, as long as each element is a two-item pair, for example [("a", 1), ("b", 2)]. A single tuple such as ("a", 1) is treated as a sequence of two elements and will raise a ValueError.
Can I use keyword arguments with update()?
Yes, for keys that are valid Python identifiers and strings, such as d.update(a=1). Keys with spaces, hyphens, or leading digits cannot be passed this way.
Does update() merge nested dictionaries?
No. It is a shallow merge. A nested dictionary in the source replaces the nested dictionary under the same key in the target rather than being combined with it.
How do I merge dictionaries without changing either one?
Create a new dictionary, for example merged = {**a, **b}, or copy one first with merged = a.copy() and then call merged.update(b).