Suppose we check for any odd numbers in list . The most direct way:
def has_odd(L): for v in L: if v % 2 == 1: return True return False
The has_odd function checks for any odd numbers in list , once an odd number is found, it returns True . But that seems a bit verbose. A more concise way to use reduce is as follows:
reduce(lambda res, v: res or bool(v), L, False)
But it will be an iteration over all elements and is not needed, because as soon as an odd number is found, the result is certainly True .
So, are there other ways to do this?
source share