Find the Product with reduce()
Intermediate · Write the code · from Python map(), filter(), reduce(), and Higher-Order Functions
From the lesson: Python map(), filter(), reduce(), and Higher-Order Functions
Picture an assembly line in a factory. Raw materials enter at one end. At the first station, each piece is shaped. At the next station, defective pieces are removed. At the final station, all the good pieces are combined into one finished product.
Read the lessonUse reduce() from the functools module to calculate the product of all numbers in the list [2, 3, 4, 5].
The product is 2 * 3 * 4 * 5 = 120.
Print the result.
Expected output:
120from functools import reduce
numbers = [2, 3, 4, 5]
# Use reduce() to multiply all numbers together
More Map, Filter and Reduce exercises
- Square Every NumberBeginner
- Filter Out Short WordsBeginner
- Predict the Pipeline OutputBeginner
- Fix the Broken PipelineIntermediate
- Build a Data PipelineAdvanced
- Refactor to a List ComprehensionAdvanced
All Python exercises · 50 practice problems · Functions quiz