Content · Programming · Python

Python Combine Data from Two Dictionaries

There are several ways to combine mappings: dictionary unpacking and | create a new shallow mapping, while update() mutates an existing one. Conflicting keys receive the later value. See two practical examples, edge.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

An application applies environment overrides to default settings.

What You Need to Understand


There are several ways to combine mappings: dictionary unpacking and | create a new shallow mapping, while update() mutates an existing one. Conflicting keys receive the later value.

When to Use It


An application applies environment overrides to default settings.

Step-by-Step Python Example


defaults = {"theme": "light", "timeout": 30}
overrides = {"timeout": 60}
result = defaults | overrides
print(result)
print(defaults)

How the Example Works


The new dictionary has timeout 60, but defaults still contains timeout 30.

Example Output


{'theme': 'light', 'timeout': 60}
{'theme': 'light', 'timeout': 30}
{'a': 3, 'b': 2}

A Second Example


Compare this smaller variation with the main example, then experiment with your own values.

base = {"a": 1}
team = {"b": 2}
user = {"a": 3}
print(base | team | user)

Important Behavior and Edge Cases


A shallow merge is not a recursive configuration merge: inner dictionaries can be replaced entirely. Choose explicit rules for nested settings.

Try It Yourself


Combine three settings layers in a predictable precedence order.

Common Beginner Mistakes


  • A shallow merge is not a recursive configuration merge: inner dictionaries can be replaced entirely. Choose explicit rules for nested settings.
  • Applying the example without a clearly defined policy for missing keys, matching identifiers, or validating external values.

Key Takeaways


  • There are several ways to combine mappings: dictionary unpacking and | create a new shallow mapping, while update() mutates an existing one.
  • The new dictionary has timeout 60, but defaults still contains timeout 30.
  • A shallow merge is not a recursive configuration merge: inner dictionaries can be replaced entirely.
Share this resourceSend this page to someone who may find it useful.