taking intersection of N-many lists in python

Posted by user248237 on Stack Overflow See other posts from Stack Overflow or by user248237
Published on 2010-06-01T20:57:51Z Indexed on 2010/06/01 21:03 UTC
Read the original article Hit count: 275

Filed under:
|
|
|

what's the easiest way to take the intersection of N-many lists in python?

if I have two lists a and b, I know I can do:

a = set(a)
b = set(b)
intersect = a.intersection(b)

but I want to do something like a & b & c & d & ... for an arbitrary set of lists (ideally without converting to a set first, but if that's the easiest / most efficient way, I can deal with that.)

I.e. I want to write a function intersect(*args) that will do it for arbitrarily many sets efficiently. What's the easiest way to do that?

EDIT: My own solution is reduce(set.intersection, [a,b,c]) -- is that good?

thanks.

© Stack Overflow or respective owner

Related posts about python

Related posts about lists