I have a list of sets:
setlist = [s1,s2,s3...]
I want s1 ∩ s2 ∩ s3 ...
I can write a function to do it by performing a series of pairwise
Is there a recommended, better, or built-in way?
From Python version 2.6 on you can use multiple arguments to
u = set.intersection(s1, s2, s3)
If the sets are in a list, this translates to:
u = set.intersection(*setlist)
*a_list is list expansion
As of 2.6,
set.intersection takes arbitrarily many iterables.
>>> s1 = set([1, 2, 3]) >>> s2 = set([2, 3, 4]) >>> s3 = set([2, 4, 6]) >>> s1 & s2 & s3 set() >>> s1.intersection(s2, s3) set() >>> sets = [s1, s2, s3] >>> set.intersection(*sets) set()