Get non-matching data when comparing python dicts and store them -
i have 2 python dicts... i'm checking if match. how can non-matched keys , values items , store them?
data1 = {'first_name': 'john', 'last_name': 'doe', 'username': 'johndoe'} data2 = {'first_name': 'john', 'last_name': 'doe', 'username': 'johohoho'} (key, value) in set(data1.items()) & set(data2.items()): print(key, value) # returns matching data. how can grab non matched?
thank in advance!
you can use xor non matching entries catching matched entries:
>>> (key, value) in set(data1.items()) ^ set(data2.items()): ... print(key, value) ... ('username', 'johndoe') ('username', 'johohoho')
note: won't work nested dictionaries
Comments
Post a Comment