0

I have a list of lists, like follow:

list = [[1,2,3],[2,3,4],[3,4,5],[3,5,6]]

I want to find the intersection of them in python 2.7, I mean

intersect_list = [3]

Thanks.

user137927
  • 347
  • 3
  • 13

1 Answers1

12

First, don't use list as a variable name - it hides the built in class.

Next, this will do it

>>> a = [[1,2,3],[2,3,4],[3,4,5],[3,5,6]]
>>> set.intersection(*map(set,a))
{3}

The map(set,a) simply converts it to a list of sets. Then you just unpack the list and find the intersection.

If you really need the result as a list, just wrap the call with list(...)

kdopen
  • 8,032
  • 7
  • 44
  • 52