Skip to content

Instantly share code, notes, and snippets.

@pford68
Created March 6, 2026 02:55
Show Gist options
  • Select an option

  • Save pford68/4a91ce308aad38690c24bdabb609a8d5 to your computer and use it in GitHub Desktop.

Select an option

Save pford68/4a91ce308aad38690c24bdabb609a8d5 to your computer and use it in GitHub Desktop.
How to merge two dictionaries in Python #python
  • As of Python 3.9, you can use the | symbol to merge them into a new dictionary. dict3 = dict1 | dict2
  • Or you can use |= to copy the right operand into the left operand. dict1 |= dict2

The older alternative is to use the unpack operator (**) which works (like the spread operator in JavaScript. Unpack of the dictionaries into a new dictionary, as shown below.

dict1 = {"a": 1, "b": 2}
dict2 = {"c": 3, "d": 4}
merged_dict = {**dict1, **dict2}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment