How do I merge two dictionaries in a single expression in Python?

parisa.tabriz
Parisa Tabriz
Published on Oct, 28 2024 1 min read 1 comments
image

I want to merge two dictionaries into a new dictionary.

a = {'x': 1, 'y': 5}
b= {'y': 3, 'z': 4}

>>> c
{'x': 1, 'y': 3, 'z': 4}

Whenever a key y is present in both dictionaries, only the value b[y] should be kept.

1 Comments

Mahabubur Rahman Oct 28, 2024 - 05:29 PM