Reduce() function in python (DS300123)

The reduce() function in Python is used to apply a given function to the elements of an iterable and reduce them to a single value. It repeatedly applies the specified function to the elements of the sequence, reducing the sequence to a single value.


from functools import reduce

Example: Finding the product of a list of numbers using reduce()

numbers = [1, 2, 3, 4, 5]
multiply = lambda x, y: x * y

Use reduce to apply the multiply function to the list of numbers

product = reduce(multiply, numbers)

Output: 120

the reduce() function returns the product of all the numbers in the list (i.e., 1 * 2 * 3 * 4 * 5 = 120).

1 Like