Having some fun with functools.reduce to accomplish this in one pass over the list.
lst = ['', 'a', 'b', '']
reduce(lambda acc, x: (acc[0], True) if x != '' else (True, acc[1]),
lst, (False, False))
# (True, True)
all(reduce(lambda acc, x: (acc[0], True) if x != '' else (True, acc[1]),
lst, (False, False)))
# True
Of course, this does have to evaluate every element in the list and cannot short-circuit. To do that, we might implement a function reduce_until which lets us supply a function that provides a condition for early exit.
def reduce_until(f, cond, iter, init):
for i in iter:
if cond(init): return init
init = f(init, i)
return init
reduce_until(lambda acc, x: (acc[0], True) if x != '' else (True, acc[1]),
all, lst, (False, False))
# (True, True)
all(reduce_until(lambda acc, x: (acc[0], True) if x != '' else (True, acc[1]),
all, lst, (False, False))
# True
float('NaN')doing here? Your list only has strings.