There are different ways to merge two dictionaries in a single expression in Python and this depends on the Python version. Here is an example of how to merge two directories in Python –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
dict1 = {"name": "debjeet", "age": 35} dict2 = {"name": "debjeet", "sex": "Male"} ## Python version 2, (or 3.4 or lower) dict3 = dict1.copy() dict3.update(dict2) print(dict3) ## {'name': 'debjeet', 'age': 35, 'sex': 'Male'} ## Python version 3.5 or higher dict3 = {**dict1, **dict2} print(dict3) ## Python version 3.9 or higher dict3 = dict1 | dict2 print(dict3) |