Back
+10 XP
Concept

Dictionary Merging: Pipe `|` Operator

Python 3.9+ merges dictionaries cleanly using the pipe operator `|`. Keys on the right side overwrite matching keys on the left.

python
defaults = {"theme": "light", "lang": "en"}
user_pref = {"theme": "dark"}
final = defaults | user_pref
print(final)  # {'theme': 'dark', 'lang': 'en'}

# 84. Dictionary Merging: Pipe `|` Operator Python 3.9+ merges dictionaries cleanly using the pipe operator `|`. Keys on the right side overwrite matching keys on the left: ```python defaults = {"theme": "light", "lang": "en"} user_pref = {"theme": "dark"} final = defaults | user_pref print(final) # {'theme': 'dark', 'lang': 'en'} ``` Before Python 3.9, developers had to use ugly hacks like `{**defaults, **user_pref}` or `.update()`. The `|` operator is clean, expressive, and immutable.

Checkpoint

In `a | b`, when both dictionaries have the same key, which one wins?

Practice

Merge `base = {"a": 1, "b": 2}` and `override = {"b": 99}` using the `|` operator into `merged`. Print `merged`.

Terminal
01
Tap code blocks below to assemble
Available Blocks