In this python dictionary program, we will add two dictionaries if the keys are the same then add their value.
Steps to solve the program
- Take two dictionaries as input.
- Using a nested for loop and an if-else statement add values of the common keys between the two dictionaries.
- Print the output.
Dict1 = {'x':10,'y':20,'c':50,'f':44 }
Dict2 = {'x':60,'c':25,'y':56}
for key in Dict1:
for k in Dict2:
if key == k:
a = Dict1[key]+Dict2[k]
Dict2[key] = a
else:
pass
Dict2
Output :
{'x': 70, 'c': 75, 'y': 76}