Merging dictionaries is a common task in Python programming. Suppose you have two separate dictionaries and want to combine them into one. In Python 3.9 and later, this is very easy thanks to the union operator (|).
|
1 2 3 4 5 6 7 8 |
# Two sample dictionaries dic_1 = {'x1': 1, 'x2': 2} dic_2 = {'y1': 3, 'y2': 4} # Merge them using the union operator dic_merged = dic_1 | dic_2 print(dic_merged) |
Output:
|
1 |
{'x1': 1, 'x2': 2, 'y1': 3, 'y2': 4} |
Last Updated on 2025-08-23 by gantovnik
Recent Comments