Python program to combine two dictionary adding values for common keys
d1 = {'a': 100, 'b': 200, 'c':300}
d2 = {'a': 300, 'b': 200, 'd':400}
d=dict()
for i in d2:
if i in d2:
d[i]=d1[i]+d2[i]
else:
d.update(d1)
d.update(d2)
print(d)
d1 = {'a': 100, 'b': 200, 'c':300}
d2 = {'a': 300, 'b': 200, 'd':400}
d=dict()
for i in d2:
if i in d2:
d[i]=d1[i]+d2[i]
else:
d.update(d1)
d.update(d2)
print(d)
Comments
Post a Comment