a=[1,2,5]
b=[3,4]
x=8
res= (x not in a) and (x not in b) #True
But what is the nice and fastest way to check it?
a=[1,2,5]
b=[3,4]
x=8
res= (x not in a) and (x not in b) #True
But what is the nice and fastest way to check it?
While your solution is perfectly fine and readable, you can make it more generic by allowing a variable number of list and checking if a given element is in any of them by wrapping it in a function:
>>> def not_in_any(*lists, key=None):
for lst in lists:
if key in lst:
return False
return True
>>> not_in_any([2, 5, 7], [8, 9, 23], [34, 56, 78], [32, 91, 6], key=32)
False
>>> not_in_any([2, 5, 7], [8, 9, 23], [34, 56, 78], [31, 91, 6], key=32)
True
>>>
Note however that Python already provides a built in function - any()
which already provides the behavior of the for-loop in our function:
def not_in_any(key=None, *lists):
not any(key in l for l in lists)
a=[1,2,5]
b=[3,4]
x=8
all(x not in i for i in (a, b))
OR:
from itertools import chain
x not in chain(a, b)
Other approach is concat and check:-
a=[1,2,5]
b=[3,4]
x=8
res= x not in a+b #True