-1

I have 2 dictionaries and here is the dictionaries:

a = [{'filtered':'eat','lang':'en'},{'filtered':'drink','lang':'en'},{'filtered':'makan','lang':'id'},{'filtered':'minum','lang':'id'}]
b = [{'filtered':'drink','lang':'en'},{'filtered':'makan','lang':'id'},{'filtered':'tidur','lang':'id'}]

I want to intersect those 2 dictionaries with the value of 'filtered' as the filtering variable and i want the result to be like this:

result = [{'filtered':'drink','lang':'en'},{'filtered':'makan','lang':'id'}]

What is the code to intersect dictionary in list like that? Thank you for your respond.

1 Answers1

0

Simply:

>>> [v for v in a if v in b]
    [{'filtered': 'drink', 'lang': 'en'}, {'filtered': 'makan', 'lang': 'id'}]

Note that this is an O(N^2) operation, but it's nontrivial to speed it up since dicts aren't hashable in Python.

AKX
  • 152,115
  • 15
  • 115
  • 172