Python Example | Python reduce() Function Explained

This python example code will show us how to use python reduce() function correctly.

Python reduce() is defined as:

functools.reduce(myfunction, iterable, initializer)

It accepts a function and a sequence and returns a single value.

from functools import reduce

def add(x, y):
    return x + y

list = [2, 4, 7, 3]
print(reduce(add, list))

We will get:

16