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

Advertisements

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'}

>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'}

Leave a Reply Cancel reply