Midway in terms of performence between not in and all. Note that the sensible (go-to) version with all for this particular case will end up performing slow - but at least ahead of min
def assert_all_not_none(l):
for x in l:
if x is None:
return False
return True
Edit: here are some benchmarks for those intersted
from timeit import timeit
def all_not_none(l):
for b in l:
if b is None:
return False
return True
def with_min(l):
min(l, key=lambda x: x is not None, default=False)
def not_in(l):
return None not in l
def all1(l):
return all(i is not None for i in l)
def all2(l):
return all(False for i in l if i is None)
def all_truthy(l):
return all(l)
def any1(l):
return any(True for x in l if x is None)
l = ['a', 'b', 'c'] * 20_000
n = 1_000
# 0.63
print(timeit("all_not_none(l)", globals=globals(), number=n))
# 3.41
print(timeit("with_min(l)", globals=globals(), number=n))
# 1.66
print(timeit('all1(l)', globals=globals(), number=n))
# 0.63
print(timeit('all2(l)', globals=globals(), number=n))
# 0.63
print(timeit('any1(l)', globals=globals(), number=n))
# 0.26
print(timeit('all_truthy(l)', globals=globals(), number=n))
# 0.53
print(timeit('not_in(l)', globals=globals(), number=n))
Surprisingly the winner: all(list). Therfore, if you are certain list will not contain falsy values like empty string or zeros, nothing wrong with going with that.
assert [a, b, c] is not Nonewill pass even if all the elements areNone. The only thing thatis Noneis...None.ismeans the same object, not an equality check. It also is not possible to create more instances ofNone's type.None. Any list is not None, in fact, any object exceptNoneis not None.