Wednesday, January 8, 2025
HomeProgrammingreduce() in Python

reduce() in Python

The reduce() function in Python, from the functools module, applies a function cumulatively to items in an iterable, reducing it to a single value.

Syntax:

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

Example:

python
from functools import reduce
nums = [1, 2, 3, 4]
result = reduce(lambda x, y: x + y, nums) # Output: 10

It’s useful for operations like summing, multiplying, or combining data.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Types of Computers

Top 10 Lawyers in India

Recent Comments

0
Would love your thoughts, please comment.x
()
x