[Solved] intersection of two or more lists of dicts


You can convert the list of dicts to set of tuples of dict items so that you can use functools.reduce to perform set.intersection on all the sets, and then convert the resulting sequence of sets to a list of dicts by mapping the sequence to the dict constructor:

from functools import reduce
def intersection(*lists):
    return list(map(dict, reduce(set.intersection, ({tuple(d.items()) for d in l} for l in lists))))

so that with your sample input:

print(intersection(l1, l2))
print(intersection(l2, l3))
print(intersection(l1, l2, l3))
print(intersection(l1, l2, l3, l4))

would output:

[{'value': 1, 'label': 'One'}, {'value': 2, 'label': 'Two'}]
[{'value': 1, 'label': 'One'}]
[{'value': 1, 'label': 'One'}]
[]

solved intersection of two or more lists of dicts