How to check if one of the following items is in a list? How to check if one of the following items is in a list? python python

How to check if one of the following items is in a list?


>>> L1 = [2,3,4]>>> L2 = [1,2]>>> [i for i in L1 if i in L2][2]>>> S1 = set(L1)>>> S2 = set(L2)>>> S1.intersection(S2)set([2])

Both empty lists and empty sets are False, so you can use the value directly as a truth value.


Ah, Tobias you beat me to it. I was thinking of this slight variation on your solution:

>>> a = [1,2,3,4]>>> b = [2,7]>>> any(x in a for x in b)True


Maybe a bit more lazy:

a = [1,2,3,4]b = [2,7]print any((True for x in a if x in b))


matomo