Python Program to Compare Two Dictionaries Keys and Values

  • Python
  • 1 min read

Here is an example of a Python program to compare two dictionaries keys and values.

Compare Two Dictionaries Keys and Values in Python

a = {
   'Apple' : 10,
   'Orange' : 20,
   'Mango' : 30
}

b = {
   'Grapes' : 100,
   'Apple' : 110,
   'Orange' : 20
}

print('Common keys:', a.keys() & b.keys())
print('Keys in a not in b:', a.keys() - b.keys())
print('(key,value) pairs in common:', a.items() & b.items())

Output:

Common keys: {'Orange', 'Apple'}
Keys in a not in b: {'Mango'}
(key,value) pairs in common: {('Orange', 20)}

Process finished with exit code 0

See also: