Add two dictionaries if the keys are the same then add their value.

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
  1. Take two dictionaries as input.
  2. Using a nested for loop and an if-else statement add values of the common keys between the two dictionaries.
  3. 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}
				
			

check whether a dictionary is empty or not.

print all the unique values in a dictionary.

Leave a Comment