Learn about functools.reduce( ) and itertools.accumulate( ) in python.

reduce() vs accumulate
reduce()
The functools
module is for higher-order functions. Functions that act on or return other functions. In general, any callable object can be treated as a function for the purposes of this module.
The functools module provides the following function functools.reduce()
functools.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.
functools.reduce(function, iterable,initializer)
If the optional initializer
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 initializer
is not given and iterable contains only one item, the first item is returned.
Example :reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])

Example 1: Find the product of the list elements using reduce()
from functools import reduce num1=[1,2,3,4,5] num2=reduce(lambda x,y:x*y,num1) print (num2)#Output:120
Example 2:Find the largest number in the iterable using reduce()
from functools import reduce num1=[15,12,30,4,5] num2=reduce(lambda x,y: x if x>y else y,num1) print (num2)#Output:30
Pictorial representation:

Example 3:Using User-defined function in reduce()
from functools import reduce def sum1(x,y): return x+y num1=[15,12,30,4,5] num2=reduce(sum1,num1) print (num2)#Output:66
Example 4: Initializer is mentioned.
If the optional initializer
is present, it is placed before the items of the iterable in the calculation.
from functools import reduce def sum1(x,y): return x+y num1=[1,2,3,4,5] num2=reduce(sum1,num1,10) print (num2)#Output:25
Example 5:Iterable contains only one item, reduce() will return that item.
from functools import reduce def sum1(x,y): return x+y num1=[5] num2=reduce(sum1,num1) print (num2)#Output:5 from functools import reduce num1=[15] num2=reduce(lambda x,y: x if x>y else y,num1) print (num2)#Output:15
Example 6: If iterable is empty and the initializer is given, reduce() will return the initializer.
from functools import reduce def sum1(x,y): return x+y num1=[] num2=reduce(sum1,num1,10) print (num2)#Output:10
itertools.accumulate()
Makes an iterator that returns accumulated sum or accumulated results of other binary functions which is mentioned in func-parameter.
If func is supplied, it should be a function of two arguments. Elements of the input iterable may be any type that can be accepted as arguments to func.-Python documentation
itertools.accumulate(iterable[, func, *, initial=None])
Example 1:By using itertools.accumulate(), we can find the running product of an iterable. The function argument is given as operator.mul.
It will return an iterator that yields all intermediate values. We can convert to list by using a list() constructor.
from itertools import accumulate import operator num2=accumulate([1,2,3,4,5],operator.mul) print (list(num2)) #Output:[1, 2, 6, 24, 120]
Pictorial Representation.

Example 2: If the function parameter is not mentioned, by default it will perform an addition operation
It will return an iterator that yields all intermediate values. We can convert to a list by using list() constructor.
import itertools #using add operator,so importing operator module import operator num1=itertools.accumulate([1,2,3,4,5]) print (num1)#Output:<itertools.accumulate object at 0x02CD94C8> #Converting iterator to list object. print (list(num1))#Output:[1, 3, 6, 10, 15]
#using reduce() for same function from functools import reduce r1=reduce(operator.add,[1,2,3,4,5]) print (r1)#Output:15
Example 3:Function argument is given as max(), to find a running maximum
from itertools import accumulate num4=accumulate([2,4,6,3,1],max) print (list(num4))#Output:[2, 4, 6, 6, 6]
Example 4: If the initial value is mentioned, it will start accumulating from the initial value.
from itertools import accumulate import operator #If initial parameter is mentioned, it will start accumulating from the initial value. #It will contain more than one element in the ouptut iterable.
num2=accumulate([1,2,3,4,5],operator.add,initial=10) print (list(num2))#Output:[10, 11, 13, 16, 20, 25]
Example 5: If the iterable is empty and the initial parameter is mentioned, it will return the initial value.
from itertools import accumulate import operator num2=accumulate([],operator.add,initial=10) print (list(num2))#Output:[10]
Example 6: If iterable contains one element and the initial parameter is not mentioned, it will return that element.
from itertools import accumulate num2=accumulate([5],lambda x,y:x+y) print (list(num2))#Output:[5]
Example 7: Iterating through the iterator using for loop
Return type is an iterator. We can iterate through iterator using for loop also.
from itertools import accumulate num2=accumulate([5,6,7],lambda x,y:x+y) for i in num2: print (i) ''' Output: 5 11 18 '''
Differences between reduce() and accumulate()

Conclusion:
- reduce() function is supported by the functools module.
- accumulate() function is supported by the itertools module.
- reduce() will return only an accumulated value.
- accumulate() will return the running accumulated value. Like we can find running max, running product, running sum of an iterable using the accumulate() function.
- accumulate() returns an iterator.
Thank you for reading my article, I hope you found it helpful!
My blog links
Resources(Python Documentation):
Make a one-time donation
Make a monthly donation
Make a yearly donation
Choose an amount
Or enter a custom amount
Your contribution is appreciated.
Your contribution is appreciated.
Your contribution is appreciated.
Buy Me a CoffeeBuy Me a CoffeeBuy Me a Coffee