Python Merge Two Dicts with Overwrite

Language:
Python
6 views
0 favorites
3 hours ago

Code Implementation

Python
dict1 = {"a": 1, "b": 2}
dict2 = {"b": 3, "c": 4}

merged = {**dict1, **dict2}
print(merged)  # {"a": 1, "b": 3, "c": 4}

Merge two dictionaries, where the latter overwrites the same keys of the former.

#dict

Snippet Description

  • Python 3.5+ supports ** to unpack and merge dictionaries
  • If keys are duplicated, the latter value will overwrite the former one
  • Python 3.9+ also allows using: dict1 | dict2

Comments

Loading...