-2

Is it possible to compare a list containing an unknown number of lists with equal elements in a more terse (aka shorter) manner than what I have done? Preferably an one-liner!

Here's an example if it's unclear what I want to do:

a = [1, 2, 3]
b = [4, 2, 1]
c = [7, 5, 1]
d = [a, b, c]

def multiCompList(lists):
    final = [i for i in lists[0] if i in lists[1]]
    for i in range(2, len(lists)):
        final = [i for i in final if i in lists[i]]

    return final

print(multiCompList(d))

What I've done is to first check if the first and second list contains any equal elements and put them in a list called final. Thereafter, checking if those elements can be found in the lists after and replacing the final-list with the remaining equal elements. The results in this case is: [1].

Carl Smestad
  • 119
  • 7

1 Answers1

0

A oneliner would look like this:

set.intersection(*[set(x) for x in d])
#set([1])
zipa
  • 27,316
  • 6
  • 40
  • 58