Skip to content

functools

jasper-zanjani edited this page Aug 6, 2020 · 1 revision

For higher-order functions (functions that act on or return other functions)
Apply function of two arguments cumulatively to the items of iterable in order to reduce it to a single value

functools.reduce(function, iterable [, initializer])

Calculate ((((1+2) +3) +4) +5)

functools.reduce(lambda x, y: x+y, [1,2,3,4,5])

functools.reduce(lambda a,b: a*b, range(1,6)) => 120 : factorial