Built-in Function functools.reduce
- VC Healy
- May 24, 2020
- 1 min read
functools.reduce(function,iterable[,initialiser]
This is the Python3 replacement for reduce()
reduce()
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.
For example,
import functools.reduce as reduce
reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
This calculates ((((1+2)+3)+4)+5).
The left argument, x, is the accumulated value and the right argument, y, is the update value from the iterable.
If the optional initialiser is present, it is placed before the items of the iterable in the calculation, and serves as a default when the iterable is empty.
If initialise is not given and iterable contains only one item, the first item is returned.
Roughly equivalent to:
def reduce(function, iterable, initializer=None):
it = iter(iterable)
if initializer is None:
try:
initializer = next(it)
except StopIteration:
raise TypeError('reduce() of empty sequence with no initial value')
accum_value = initializer
for x in it:
accum_value = function(accum_value, x)
return accum_value
Comments