The code will work irrespective of whether d1
or d2
have the same set of keys. I have added a key 'e'
in d1
and 'd'
in d2
.
d1 = {'a': [2, 4, 5, 6, 8, 10], 'b': [1, 2, 5, 6, 9, 12], 'c': [0, 4, 5, 8, 10, 21], 'e':[0,0,0]}
d2 = {'a': [12, 15], 'b': [14, 16], 'c': [23, 35], 'd': [13, 3]}
d2_keys_not_in_d1 = d2.keys() - d1.keys()
d1_keys_not_in_d2 = d1.keys() - d2.keys()
common_keys = d2.keys() & d1.keys()
for i in common_keys:
d[i]=d1[i]+d2[i]
for i in d1_keys_not_in_d2:
d[i]=d1[i]
for i in d2_keys_not_in_d1:
d[i]=d2[i]
d
{'a': [2, 4, 5, 6, 8, 10, 12, 15],
'b': [1, 2, 5, 6, 9, 12, 14, 16],
'c': [0, 4, 5, 8, 10, 21, 23, 35],
'd': [13, 3],
'e': [0, 0, 0]}