Update Dictionary in Python Syntax

How-to basics · Updated November 23, 2026

To update a dictionary in Python, use the dict.update() method or direct key assignment: my_dict.update({'key': 'value'}) or my_dict['key'] = 'value'. The update() method merges another dictionary, an iterable of key-value pairs, or keyword arguments into the existing dictionary, overwriting any keys that already exist.

Background: How Python Dictionaries Work

Dictionaries (dict) are built-in, unordered (in Python 3.6+ they maintain insertion order as an implementation detail) collections that store key-value pairs. Each key must be unique and hashable (strings, numbers, tuples of immutable types). Values can be any object. You create one with curly braces: d = {'a': 1, 'b': 2}. Updating is a core operation because dictionaries are mutable—you can change them in place without reassigning the variable. This differs from strings and tuples, which are immutable and require creating new objects.

Direct Assignment vs. update() Method

The simplest way to add or change a single key is direct assignment: d['key'] = value. If the key exists, the value is replaced; if not, a new key-value pair is added. This is fast and clear for one key. For multiple keys or merging another dictionary, use update(). It accepts:

  • Another dictionary: d.update({'x': 1, 'y': 2})
  • An iterable of key-value pairs (e.g., list of tuples): d.update([('x', 1), ('y', 2)])
  • Keyword arguments: d.update(x=1, y=2) (keys must be strings)

Both methods overwrite existing keys. Neither returns a new dictionary—they modify the original in place and return None. If you need a copy, use d.copy() or {**d, **new} (Python 3.5+) to create a new dict with merged contents.

Merging Two Dictionaries (Python 3.9+)

Python 3.9 introduced the union operator | and augmented assignment |= for dictionaries. Example: merged = d1 | d2 returns a new dict with keys from both, with d2’s values overriding d1’s on conflicts. d1 |= d2 updates d1 in place, equivalent to d1.update(d2). This is more readable for merging than chaining update() calls. For earlier versions, use {**d1, **d2} (unpacking) to merge into a new dict.

Common Problems and Misconceptions

A frequent mistake is assuming update() returns the updated dictionary—it returns None, so d = d.update(...) sets d to None. Another issue: using update() with keyword arguments fails if your key is not a valid identifier (e.g., d.update({'a-b': 1}) is fine, but d.update(a-b=1) is a syntax error). Also, remember that update() overwrites entire values; it does not merge nested dictionaries. For nested merging, you need a recursive function or the collections.ChainMap for read-only views. A common misconception is that dictionaries are sorted—they are not; order is insertion order, but updating an existing key does not change its position.

Performance and When to Use Which

Direct assignment is O(1) average time. update() is O(k) where k is the number of keys being added. For single keys, assignment is slightly faster because it avoids method call overhead. For merging large dictionaries, update() is efficient because it iterates the source once. The | operator creates a new dict, which is O(n+m) memory and time—use it when you need to keep the originals unchanged. If you are updating in a loop, avoid update() with a single key each iteration; use assignment. For bulk updates from a database or API response, update() is idiomatic.

Updating Nested Dictionaries and Defaults

To update a value inside a nested dict, you must access the inner dict first: d['outer']['inner'] = new_value. If the outer key doesn’t exist, you’ll get a KeyError. Use setdefault() to avoid this: d.setdefault('outer', {})['inner'] = value creates the outer dict if needed. For counting or accumulating, use collections.defaultdict with a factory like defaultdict(int) so that missing keys automatically get a default value when you do d['key'] += 1. For merging nested dicts deeply, write a helper function that recurses into dict values.

Alternatives and Related Methods

Beyond update(), you have setdefault(key, default) which inserts the key only if missing, returning the existing or default value. pop(key, default) removes and returns a value, with a fallback. clear() empties the dict. For read-only merging, collections.ChainMap chains dicts without copying. In Python 3.8+, you can use the walrus operator in comprehensions to build updated dicts. The dict constructor itself can update: dict(existing, new_key=value) creates a new dict, but only for string keys.

Frequently Asked Questions

How do I update a dictionary with another dictionary? Use d1.update(d2) to modify d1 in place, or merged = d1 | d2 (Python 3.9+) to get a new dict. Both overwrite keys that exist in d1 with values from d2.

What is the difference between update() and assignment? Assignment (d['k']=v) handles one key at a time and is slightly faster. update() can take multiple keys from a dict, list of tuples, or keyword arguments, making it better for bulk changes. Both modify the original dict.

Does update() return a new dictionary? No, it returns None. It modifies the dictionary in place. If you need a new dict, use d.copy() first, then update the copy, or use the | operator.

Can I update a dictionary with a list of tuples? Yes, pass a list of (key, value) pairs to update(): d.update([('a',1),('b',2)]). Each tuple must have exactly two elements.

Why does my update() not work when using a variable as key? It does work. The issue is likely that you’re using keyword arguments with a variable name that isn’t a string literal—use a dict instead: d.update({my_key: value}). Keyword arguments require the key to be a valid Python identifier.

How to update a dictionary without overwriting existing keys? Use setdefault() for each key, or check if key not in d: d[key] = value. For merging with a default, loop through the source dict and only add missing keys.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.