I know this is an old question, but I wanted to add an answer which I believe is better.
If all elements which have to be checked are hashable, you could use a set instead of a list or tuple.
>>> None not in {1, 84, 'String', (6, 'Tuple'), 3}
This is much faster than the methods in the other answers.
$ python3 -m timeit "all(v is not None for v in [1, 84, 'String', (6, 'Tuple'), 3])"
200000 loops, best of 5: 999 nsec per loop
$ python3 -m timeit "None not in [1, 84, 'String', (6, 'Tuple'), 3]"
2000000 loops, best of 5: 184 nsec per loop
$ python3 -m timeit "None not in (1, 84, 'String', (6, 'Tuple'), 3)"
2000000 loops, best of 5: 184 nsec per loop
python3 -m timeit "None not in {1, 84, 'String', (6, 'Tuple'), 3}"
5000000 loops, best of 5: 48.6 nsec per loop
Another advantage of this method is that it gives you the correct answer even if someone defines the __eq__
method of a class to always return True
. (Of course, if they define the __hash__
method to return hash(None)
, this method won't work. But nobody should do that, because it would defeat the purpose of defining a hash.)
class Int(int):
def __eq__(self, other):
return True
def __hash__(self):
return hash(super())
print(all(v is not None for v in [1, Int(6), 2])) # True (correct)
print(None not in [1, Int(6), 2]) # False (wrong)
print(None not in (1, Int(6), 2)) # False (wrong)
print(None not in {1, Int(6), 2}) # True (correct)