Press "Enter" to skip to content

Python Program to Compare Two Dictionaries Keys and Values

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:

Vinish Kapoor

Hi, I am a full stack developer and writing about development. I document everything I learn and help thousands of people. foxinfotech.in is created, written, and maintained by me; it is built on WordPress, and hosted by Bluehost. Connect with me on Facebook, Twitter, GitHub, and get notifications for new posts.

Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *