# reduce()

reduce() method

reduce() — used to apply a particular function passed in its argument to all of the list elements mentioned in the sequence passed along.

reduce(function, iterable[, initializer])
from functools import reduce

numbers = [3, 4, 6, 9, 34, 12]

def custom_sum(first, second):
return first + second

result = reduce(custom_sum, numbers)
# python code to demonstrate working of reduce()

# importing functools for reduce()
import functools

# initializing list
lis = [ 1 , 3, 5, 6, 2, ]

# using reduce to compute sum of list
print ("The sum of the list elements is : ",end="")
print (functools.reduce(lambda a,b : a+b,lis))

# using reduce to compute maximum element from list
print ("The maximum element of the list is : ",end="")
print (functools.reduce(lambda a,b : a if a > b else b,lis))

returns a single value.

reduce()

## reduce() — Structure map

Clickable & Draggable!