0

How can one easily find the common values between more than two lists?

Exemple:

Lists to match:

L1 = [1,2,3,4,5]
L2 = [4,5,6,7,8]
L3 = [9,10,11,12,4]
L4 = [13,10,12,4]

Would return:

L5 = [4]    

Note: something like the following code would take far too long:

def search(a,b,c,d):                          
    my_list=[]
    for i in a:
        for i in b:
            for i in c: 
                for i in d:
                    if i in a and i in b and i in c and i in d: 
                        id_sel.append(i)
return (my_list)

Note: something like the following code would be appreciated:

my_list=list(set(a).intersection(b)) 
K DawG
  • 13,287
  • 9
  • 35
  • 66

1 Answers1

4

Use set.intersection():

>>> set(L1).intersection(L2, L3, L4)
set([4])
#or
>>> set.intersection(set(L1), L2, L3, L4)
set([4])
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504