0

I have 2 python dicts... I'm currently checking if they match. But how can I get the non-matched keys and values from items and store them?

data1 = {'first_name': 'John', 'last_name': 'Doe', 'username': 'johndoe'} 
data2 = {'first_name': 'John', 'last_name': 'Doe', 'username': 'johohoho'}

for (key, value) in set(data1.items()) & set(data2.items()):
        print(key, value) 
# this returns only matching data. how can i grab the non matched?

Thank you for your help in advance!

Modelesq
  • 5,192
  • 20
  • 61
  • 88

1 Answers1

1

You can use xor for non matching entries just like you are catching matched entries:

>>> for (key, value) in set(data1.items()) ^ set(data2.items()):
...         print(key, value) 
... 
('username', 'johndoe')
('username', 'johohoho')

Note: this won't work for nested dictionaries

dnit13
  • 2,478
  • 18
  • 35