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.
reduce(), it's not exactly the best idea, since it's being phased out in Python 3. It's also slower, from what I've heard, compared to a for loop.from functools import reduce. It's removed from thebuiltins, it's not phased out completely. Also, what you've heard about its speed is highly inaccurate: it can be much faster or slower than a for loop.