159k views
1 vote
How to merge two dictionaries with same keys

1 Answer

0 votes

Final answer:

To merge two dictionaries with the same keys in Python, use the update() method.

Step-by-step explanation:

To merge two dictionaries with the same keys in Python, you can use the `update()` method. This method combines the key-value pairs from the second dictionary into the first dictionary. If there are any common keys, the values from the second dictionary will overwrite the values in the first dictionary.

Example:

dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}
dict1.update(dict2)
print(dict1) # Output: {'a': 1, 'b': 3, 'c': 4}

User DEEPAK LAKHOTIA
by
7.1k points