Python Basics

Merge Dictionaries in Python with | and update()

Combine default settings with user choices in one expression, and see which value wins when keys clash.

  • Beginner
  • 2 min read
  • 1 runnable example
What you'll learn
  • Merging with the | operator (Python 3.9+)
  • Updating in place with |= and update()
  • Which value wins for duplicate keys
defaults | preferences
defaults
  • theme: dark
  • language: en
preferences
  • language: vi
  • font_size: 16
result
  • theme: dark
  • language: vi (from preferences)
  • font_size: 16

The right side wins

The problem

Settings often come from several places: defaults built into the program, then the user's own choices on top. You need one combined result where the user's choices win, without changing the defaults that other users still need.

Think of it like this

Merging dictionaries is like laying a sheet of stickers over a printed form. Where the sticker covers a field, you see the sticker's value. Where there is no sticker, the original print shows through. The form underneath is not damaged.

From stickers on a form to code
defaults📄 The printed form
preferences🏷️ The stickers on top
defaults | preferences👀 What you see: stickers win where they overlap
"language": "vi"🔁 The sticker hides the printed en
Keys in both: the right side wins. The originals are not changed.
In one sentence

The | operator merges two dictionaries into a new one; when a key is in both, the value from the right-hand dictionary is kept.

Key points

  1. defaults | preferences creates a new dictionary; neither defaults nor preferences changes.
  2. "language" is in both, so the value from the right side, "vi", wins.
  3. "theme" comes only from defaults and "font_size" only from preferences, so both are kept.
  4. To change defaults itself, use defaults.update(preferences) or defaults |= preferences.

When should you use it?

  • Applying user settings on top of default settings.
  • Combining configuration from a file, environment variables and command-line options.
  • Updating a record with only the fields that changed.

When not to use it

  • Nested settings (a dictionary inside a dictionary): | replaces the inner dictionary as a whole instead of merging it.
  • Python older than 3.9, which has no | for dictionaries: use {**defaults, **preferences}.

See how it works

Type a key and store or look it up. Python hashes the key to find its slot directly, without searching through every entry.

Example code

Run code →
main.py
defaults = {"theme": "dark", "language": "en"}
preferences = {"language": "vi", "font_size": 16}

settings = defaults | preferences
print(settings)
Get a Run button for this example

Result of running the code

{'theme': 'dark', 'language': 'vi', 'font_size': 16}

Code explanation

The union operator creates a new dictionary. When keys overlap, the value from the dictionary on the right wins.

Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.

Practice exercises

Modify the runnable example with the exercises below to build understanding beyond copying the result.

  1. Change the input and predict the output before running.
  2. Handle empty or invalid input.
  3. Wrap the logic in a function and add more test cases.
Run in the Python compiler →

Common mistakes

Putting the dictionaries in the wrong order

The right side wins. Defaults must come first.

Problem
settings = preferences | defaults  # defaults overwrite the user
Fix
settings = defaults | preferences

Changing the defaults by accident

update() changes the dictionary in place, so the next user starts with the wrong defaults.

Problem
defaults.update(preferences)
settings = defaults
Fix
settings = defaults | preferences

Finished this lesson?

Mark it as learned to track your progress.

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges