This post is about reduce. It’s higher order function part of functool
package.
The documentation describes it as
Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value.
combing lambda and reduce can be save a lot of time while processing bunch of lists. It recently saved me some time to and all expressions in list of objects. I guess it can be with list comprehension or something. who doesn’t love hight order functions! they just sound fancy.
value = reduce(lambda a, b: a and b, [node.value for node in nodes])
This small full example:
from functools import reduce
x = [ 1 , 1 , 1 ]
r = reduce(lambda a, b: a + b, x)
print(r)