Python Extend Dictionary

Learn how to extend a dictionary in Python using update(), the |= operator, the | merge operator, and dictionary unpacking. Also learn why dict has no extend() method like list.

Published

Updated

Read time 6 min read

Reviewed byDeepak Prasad

Python Extend Dictionary

Lists have extend(), but dictionaries do not have dict.extend(). That trips many beginners: you are not “appending pairs to the end” of a sequence—you are merging keys into a mapping where each key is unique.

Python dictionaries do not have an extend() method. To extend a dictionary, use update() or the |= operator if you want to modify the existing dictionary. Use the | operator or ** unpacking if you want to create a new merged dictionary.

A dictionary stores key-value pairs in curly braces {} and preserves insertion order in modern Python (for example, list(d) follows insertion order, as the Python tutorial describes). Keys are unique: assigning again replaces the previous value—there are never two entries for the same key.

Need Use
Modify an existing dictionary dict1.update(dict2)
Modify an existing dictionary (Python 3.9+) dict1 |= dict2
Create a new merged dictionary (Python 3.9+) dict3 = dict1 | dict2
Create a new merged dictionary (older Python) dict3 = {**dict1, **dict2}
Add one key-value pair dict1["key"] = value

This page focuses on the question “How do I extend an existing dictionary?” For a wider tour of merge patterns, ChainMap, copies, and related topics, see merge dictionaries in Python. For adding keys in day-to-day code, Python add to dictionary fits closely as well. Background on dict basics lives in the Python dictionary guide.

Tested on: Python 3.13.3; kernel 6.14.0-37-generic.


Can you extend a dictionary in Python?

Yes. “Extending” a dictionary means bringing keys (and their values) from another mapping into an existing dict, or building a larger dict from two or more mappings. There is no extend() name on dict because the data model is not “items in a row” like a list—it is keys pointing to values, and dictionaries are defined as key-value mappings with unique keys.


Why dict has no extend() method

list.extend(iterable) grows the list by appending elements from the iterable in order (see append vs extend in Python lists). A dict has no ordered slot you append into: you insert or overwrite by key. The closest mental model to “extend” is “merge these keys into me” (update, |=) or “give me a new dict that contains both” (|, {**a, **b}). If you need list-like behavior under one key, store a list as the value and call extend on that list.


Extend a dictionary using update()

dict.update() merges another mapping or an iterable of (key, value) pairs into the dictionary in place and returns None. If a key already exists, the incoming value replaces the old one—there is no separate “remove duplicates” step because duplicate keys cannot exist in one dict.

python
left = {"name": "Ada", "score": 10}
right = {"score": 99, "team": "blue"}
left.update(right)
print(left)
Output

You should see name unchanged, score updated to 99 from right, and team added.


Extend a dictionary using |= operator

The |= augmented assignment operator updates the left-hand dictionary in place. It was added in Python 3.9 together with | (PEP 584). For duplicate keys, the right-hand side wins, same as update().

python
a = {"x": 1}
b = {"x": 2, "y": 3}
a |= b
print(a)
Output

You should see x equal to 2 and y equal to 3. On Python 3.8 or earlier, use a.update(b) instead—|= is not available.


Merge dictionaries using | operator

The binary | operator builds a new dictionary; neither operand is modified. PEP 584 calls this the merge operator and specifies right-hand wins on key overlap.

python
first = {"a": 1, "b": 2}
second = {"b": 9, "c": 3}
merged = first | second
print(merged)
print("first unchanged:", first)
Output

You should see merged with b equal to 9, and first still {"a": 1, "b": 2}.


Extend a dictionary using unpacking

Unpacking builds a new dict literal. It works on older Python versions and does not require 3.9’s |. Later unpackings overwrite earlier keys for the same name, same “right wins” idea.

python
old_a = {"a": 1}
old_b = {"a": 2, "b": 3}
combined = {**old_a, **old_b}
print(combined)
Output

You should see a equal to 2 and b equal to 3.


Add one key-value pair to a dictionary

Use subscript assignment. New keys add a pair; existing keys replace the value.

python
cfg = {"host": "localhost"}
cfg["port"] = 8080
cfg["host"] = "127.0.0.1"
print(cfg)
Output

You should see host updated and port present.


Extend dictionary with another dictionary

The usual pattern is base.update(extra) when extra is another dict or any mapping. You can also pass keyword arguments to update for string keys that are valid identifiers.

python
base = {"id": 1}
extra = {"name": "Kim", "id": 2}
base.update(extra)
print(base)
Output

id ends up as 2 because keys from extra overwrite on conflict.


Extend dictionary without changing the original

Use first | second or {**first, **second} so you get a new dict and leave first and second untouched.

python
original = {"a": 1}
incoming = {"b": 2}
snapshot = original | incoming
print(snapshot)
print("original keys:", list(original))
Output

snapshot should contain both keys; original should still only have a.


What happens when keys already exist?

There is only one value per key. When both sides define the same key, the merge or update picks one value: for d.update(other), d |= other, d | other, and {**a, **b}, the value from the right-hand or later mapping wins (PEP 584 for | and |=). If you need to combine values (for example sum counts or concatenate lists) for the same key, you write explicit logic—plain dict operations never keep two values under one key.

python
left = {"k": [1]}
right = {"k": [2]}
# normal merge replaces the list, it does not append:
print(left | right)
Output

You should see k mapped to [2], not [1, 2].


update() vs |= vs | vs

Mechanism Mutates left dict? Result Typical Python
d.update(m) Yes None; d grows in place All supported versions
d |= m Yes None; d grows in place 3.9+
d | m No New dict 3.9+
{**d, **m} No New dict 3.5+ (dict display unpacking)

Common mistakes when extending dictionaries

  • Calling dict.extend()—that API exists on list, not on dict.
  • Forgetting that update() and |= change the original dict; assign or copy first if you need the old snapshot.
  • Forgetting that d | m returns a new dict; assigning back (d = d | m) is fine, but d | m alone does not update d.
  • Expecting duplicate keys to store two values at once—only one value per key survives.
  • Using dict(base, **extra) when extra has non-string keys or keys that are not valid keyword names; prefer update, |=, |, or {**base, **extra}.
  • Treating dictionary “extend” like list.extend—here you merge by key, not append positions.
  • Assuming a shallow merge combines nested dicts field by field; inner dicts are replaced wholesale unless you add custom merging.

Python extend dictionary quick reference table

Need Use
Extend an existing dictionary dict1.update(dict2)
Extend in place on Python 3.9+ dict1 |= dict2
New merged dictionary on Python 3.9+ dict3 = dict1 | dict2
New merged dictionary before 3.9 dict3 = {**dict1, **dict2}
Add one item dict1["key"] = value
Add several keyword-style keys dict1.update(name="Ada", score=10)
Duplicate-key behavior Right-hand / later value wins
Avoid dict1.extend(dict2)

Summary

You extend a Python dictionary by merging keys: in place with update() or |= (3.9+), or without touching inputs using | (3.9+) or {**a, **b}. There is no dict.extend() because dicts are mappings, not growable sequences. Shared keys always resolve to a single value, with the right-hand side winning in the usual merge forms.


References


Frequently Asked Questions

1. Does Python have dict.extend() like list.extend()?

No—built-in dict has no extend() method; lists are sequences and grow at the end, while dicts map unique keys to values, so you merge with update(), |= (3.9+), or build a new dict with | or ** unpacking.

2. What is the difference between dict.update() and the |= operator?

Both merge into the left-hand dictionary in place with the same duplicate-key rule (right-hand values win); |= is the augmented assignment spelling added in Python 3.9 alongside the | merge operator in PEP 584.

3. When should I use | instead of update()?

Use | when you need a new merged dictionary and must leave the originals unchanged; update() and |= modify the first dictionary without creating a new top-level dict object.

4. What happens if both dictionaries share a key?

A dict keeps only one value per key, so the later or right-hand mapping wins—PEP 584 describes union and update as keeping the rightmost value on conflicts.

5. Can I use dict(first, **second) when second has integer keys?

No—the constructor form that splats keyword arguments only accepts string keys valid as identifiers for the **kwargs path; use update(), |=, |, or {**a, **b} for arbitrary hashable keys.
Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with more than 15 years of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive …