# First falsy element or last element if all are truthy:
reduce(lambda i, j: i and j, [100, [], 20, 10]) # = []
reduce(lambda i, j: i and j, [100, 50, 20, 10]) # = 10
# First truthy element or last element if all falsy:
reduce(lambda i, j: i or j, [100, [], 20, 0]) # = 100
reduce(lambda i, j: i or j, ['', {}, [], None]) # = None
Instead of creating a lambda
-function it is generally recommended to create a named function:
def do_or(i, j):
return i or j
def do_and(i, j):
return i and j
reduce(do_or, [100, [], 20, 0]) # = 100
reduce(do_and, [100, [], 20, 0]) # = []