-4

I know this question is asked a lot, but they are all used for different things. What I want to happen:

#python 3.9
list = [["grass", "sand", "water"],["rock", "grass", "sand"]]
matches = ["sand", "water"]

Is there a way to find matches this way?

LeWolfYT
  • 3
  • 1

1 Answers1

1

Convert the lists into sets and take their intersection. You can do this across an arbitrarily long list of sets in a single line with functools.reduce:

>>> my_list = [["grass", "sand", "water"],["rock", "grass", "sand"]]
>>> import functools
>>> functools.reduce(set.intersection, map(set, my_list))
{'grass', 'sand'}
Samwise
  • 68,105
  • 3
  • 30
  • 44