One liner to get the common elements of several lists

While doing an exercise with a mockup catalog and indexes, I ran into the problem of filtering several lists and returning the common elements from the list. The following example demonstrate the usage of reduce(), one of the functional programming constructs that are less common and obvious in their usage.

>>> a = [1,2,3,4]
>>> b = [2,3,4,5]
>>> c = [1,2,4,6]
>>> d = [0,2,3]
>>> z = [a,b,c,d]
>>> z = reduce(lambda i,j:list(set(i).intersection(j)), z)
>>> z
[2]

Comments