how to merge similar values in dictionary python code example
Example 1: how to merge two dictionaries in python
>>> x = {'a': 1, 'b': 2}
>>> y = {'b': 10, 'c': 11}
>>> z = {**x, **y}
>>> print(z)
{'a': 1, 'b': 10, 'c': 11}
Example 2: python join dict
def mergeDict(dict1, dict2):
''' Merge dictionaries and keep values of common keys in list'''
dict3 = {**dict1, **dict2}
for key, value in dict3.items():
if key in dict1 and key in dict2:
dict3[key] = [value , dict1[key]]
return dict3
dict3 = mergeDict(dict1, dict2)
print('Dictionary 3 :')
print(dict3)