I have two lists of python dictionaries. I want to create a new list of similar dictionaries based on the difference between list1 and list2 using the key value 'fruit'. None of the dictionaries in either list are identical.
list1 = [
{'fruit': 'apple', 'amt': '1'},
{'fruit': 'banana', 'amt': '2'},
{'fruit': 'cherry', 'amt': '3'},
{'fruit': 'date', 'amt': '4'},
{'fruit': 'elderberry', 'amt': '5'},
]
list2 = [
{'fruit': 'date', 'amt': '6'},
{'fruit': 'elderberry', 'amt': '7'},
]
list3 = [some kind of python list comprehension]
print(list3)
{'fruit': 'apple', 'amt': '1'}
{'fruit': 'banana', 'amt': '2'}
{'fruit': 'cherry', 'amt': '3'}
It's a bit unclear if you would want list3
to contain dictionaries only from list1
if they're not in list2
or also from list2
if they're not in list1
.
But basically you want either a difference or a symmetric difference of dictionaries in list1
and list2
based on the values in key fruit
.
Difference contains items that exist only in the first set, and not in both sets.
If your lists only had the values of fruit
in them, you could create sets out of them and do the difference using python's built in function: list1.difference(list2)
. But as they're dictionaries with key-values that we want to ignore, you need to do it manually. List comprehension works:
list3 = [x for x in list1 if x['fruit'] not in [y['fruit'] for y in list2]]
I.e. for items in list1
, return item if its fruit
is not in a list of fruit
s from list2
.
Symmetric difference contains a mix of items that are not present in both sets
The list comprehension is basically the same but done both ways list1.difference(list2) + list2.difference(list1)
:
list3 = [x for x in list1 if x['fruit'] not in [y['fruit'] for y in list2]] + [x for x in list2 if x['fruit'] not in [y['fruit'] for y in list1]]