Python reduce() Function

Python reduce() function is used to accepts a function and sequence and return a single value.
This Python function is defined in functools module.

Some Examples of Python reduce function

Example to add more than values using reduce() function with two parameters

from functools import reduce
def do_addition(x, y): 
    return x + y

print(reduce(do_addition, [10, 20, 30, 40]))
Tutorials Class - Output Window

60

Example to multiply more than values using reduce() function with two parameters

from functools import reduce
def do_multiplication(x, y):
    return x * y

print(reduce(do_multiplication, [10, 2, 3, 4]))
Tutorials Class - Output Window

240


Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.