Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Issue Finding Keys in 2nd Dictionary which Has Different Value Compare to Dictionary One

I can use diff = set(dict2) - set(dict1) to know that the dict1 and dict2 are not equal but what I need is to find which KEY in dict2 has a different value than dict1

 dict1 = {'c1': 'Iran', 'c2': 'India', 'c3': 'Iraq', 'c4': 'Qatar'}
 dict2 = {'c1': 'Iran', 'c2': 'India', 'c3': 'Iraqs','c4': 'Qeter'}
 diff = set(dict2) - set(dict1)
 print(diff)

Basically what I want to get in return are {'c3', 'c4'}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can do like this,

keys = set()
for key in dict1:
    if key in dict2 and dict1[key] != dict2[key]:
        keys.add(key)

print(keys)

output:

{'c3', 'c4'}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading