Dictionaries Python 3 Update: What Changed and How to Use It

How-to basics · Updated November 23, 2026

In Python 3, dictionaries are insertion-ordered by default (since 3.7 as a language guarantee), support dictionary union operators | and |= (since 3.9), and include methods like dict.update() that merge another mapping or iterable of key-value pairs into the dictionary. The most direct answer to “Dictionaries python 3 update?” is that update() modifies the dictionary in place, adding or replacing keys with values from another dictionary, an iterable of pairs, or keyword arguments, and returns None.

Background: How dict.update() Evolved

Dictionaries have been a core Python type since version 1.0, but their behavior and methods have changed significantly across Python 3 releases. The update() method has existed since Python 2, but Python 3 refined its input handling, error messages, and performance. In Python 3.9, the language added the union operator | for dicts, which creates a new merged dictionary, while |= updates in place—similar to update() but with a different syntax. The insertion-order guarantee was an implementation detail in CPython 3.6 and became official in Python 3.7, meaning that update() preserves the order of existing keys and appends new keys at the end.

How dict.update() Works Under the Hood

The update() method accepts three types of arguments:

  • Another dictionary (or any mapping object) – keys and values are copied over.
  • An iterable of key-value pairs – typically a list of tuples or a list of two-element lists.
  • Keyword arguments – keys are strings (must be valid identifiers).

If a key already exists, its value is overwritten; if not, the key is added. The method returns None, so it must be called as a statement, not assigned. Here’s a concrete example:

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

You can also pass an iterable of pairs:

d.update([('c', 5), ('d', 6)])
print(d)  # {'a': 1, 'b': 3, 'c': 5, 'd': 6}

And keyword arguments work for string keys:

d.update(e=7, f=8)
print(d)  # {'a': 1, 'b': 3, 'c': 5, 'd': 6, 'e': 7, 'f': 8}

If you pass multiple sources (e.g., a dict and keyword args), later sources override earlier ones for the same key.

Choosing Between update(), |=, and {**d1, **d2}

Python 3.9 introduced the union operator | for dictionaries. The key difference is that update() and |= modify the original dictionary in place, while | creates a new dictionary. Use update() when you want to mutate an existing dict and don’t need the return value. Use | when you want a new merged dict without altering the originals. Use {**d1, **d2} as a pre-3.9 alternative for creating a new dict, but note that it only works with mapping objects, not iterables of pairs. For example:

d1 = {'x': 1}
d2 = {'y': 2}
merged = d1 | d2  # {'x': 1, 'y': 2}
print(merged)  # new dict
d1 |= d2  # d1 becomes {'x': 1, 'y': 2}

If you need to update from an iterable of pairs, update() is the only direct method; | requires a dict on the right side.

Common Problems and Pitfalls

One frequent mistake is expecting update() to return the updated dictionary. It returns None, so code like new_dict = old_dict.update(other) sets new_dict to None. Another pitfall is using keyword arguments with non-string keys—keyword arguments must be valid identifiers, so you cannot do d.update(1=2); use a dict or iterable instead. Also, if you pass an iterable of pairs where the pairs have more than two elements, Python raises a ValueError. For example:

d.update([(1, 2, 3)])  # ValueError: dictionary update sequence element #0 has length 3; 2 is required

Finally, be careful with mutable values: update() copies references, not the underlying objects, so modifying a list value after updating will affect the dictionary too.

Performance and Memory Considerations

In CPython 3.6+, dictionaries are implemented with a compact layout that reduces memory usage. update() is generally O(n) where n is the number of items in the source. For large merges, update() is efficient because it avoids creating an intermediate dict. However, if you need to merge many dictionaries repeatedly, consider whether you can build a single dict from multiple sources in one call, since each update() call has overhead. In Python 3.9+, the | operator also runs in C and is comparable in speed to update() for creating new dicts. For very large dictionaries, the insertion-order guarantee means that updating with an existing key does not change its position; new keys are appended at the end, which can affect iteration order if you rely on it.

Alternatives to update() in Python 3

Besides |, there are other ways to merge or update dictionaries:

  • Dictionary unpacking {**d1, **d2} – creates a new dict, works in Python 3.5+.
  • dict comprehension – for conditional merging, e.g., {k: v for k, v in d1.items() if v is not None}.
  • collections.ChainMap – for a view that searches multiple dicts without merging.
  • dict(d1, **d2) – a constructor trick that works but only for string keys.

For updating multiple keys with the same value, you can use dict.fromkeys() to create a dict, then update it. For example, to set several keys to zero:

keys = ['a', 'b', 'c']
d = {}
d.update(dict.fromkeys(keys, 0))
print(d)  # {'a': 0, 'b': 0, 'c': 0}

If you need to merge many dicts without modifying any, you can chain | in Python 3.9+ or use a loop with update() on a new dict.

Frequently Asked Questions

Does dict.update() return a new dictionary?

No. update() modifies the dictionary in place and returns None. If you need a new dict, use the | operator (Python 3.9+) or {**d1, **d2}.

Can I use update() with keyword arguments and a dict at the same time?

Yes. For example, d.update({'a': 1}, b=2) works, but note that the keyword argument keys must be valid identifiers. The dict argument is processed first, then keyword arguments, so later values override earlier ones.

What is the difference between update() and |= in Python 3.9+?

Functionally, d |= other is equivalent to d.update(other) for dicts. Both modify in place. The |= operator is syntactic sugar and can be used in augmented assignment expressions. There is no performance difference in most cases.

Does update() preserve the order of keys?

Yes, since Python 3.7, dictionaries maintain insertion order. Updating an existing key does not change its position; adding a new key appends it at the end. This behavior is guaranteed by the language specification.

Can I update a dict from a list of tuples with duplicate keys?

Yes, but later pairs override earlier ones. For example, d.update([('a', 1), ('a', 2)]) results in {'a': 2}.

Is there a way to update a dict only if the key doesn’t exist?

Not directly with update(). Use setdefault() or check membership first: if key not in d: d[key] = value. Alternatively, use d.setdefault(key, value) which sets only if missing.

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.