Sunday, 30 August 2026

๐Ÿš€ Day 98/150 – reduce() Function in Python

 



๐Ÿš€ Day 98/150 – reduce() Function in Python

The reduce() function is used to repeatedly apply a function to the elements of an iterable until a single value is produced. Unlike map() and filter(), reduce() returns one final result instead of another iterable.

The reduce() function is available in Python's functools module.

Syntax:

from functools import reduce 
reduce(function, iterable)

In this post, we'll explore four common examples of using the reduce() function in Python.


Method 1 – Using reduce() with a Normal Function

Find the sum of all numbers in a list.

from functools import reduce def add(x, y): return x + y numbers = [1, 2, 3, 4, 5] result = reduce(add, numbers) print(result)








Output

15

Explanation

  • add() takes two numbers and returns their sum.

  • reduce() repeatedly applies the function to the list.

  • Calculation:

      (1 + 2) = 3

      (3 + 3) = 6

      (6 + 4) = 10

      (10 + 5) = 15
    • The final result is 15.


Method 2 – Using reduce() with a Lambda Function

Find the product of all numbers in a list.

from functools import reduce numbers = [1, 2, 3, 4, 5] result = reduce(lambda x, y: x * y, numbers) print(result)








Output
120

Explanation

  • lambda x, y: x * y multiplies two numbers.

  • reduce() applies the lambda function repeatedly.

  • Calculation:


    (1 × 2) = 2


    (2 × 3) = 6


    (6 × 4) = 24


    (24 × 5) = 120

Method 3 – Find the Maximum Value

Use reduce() to find the largest element in a list.

from functools import reduce numbers = [12, 45, 7, 89, 23] maximum = reduce(lambda x, y: x if x > y else y, numbers) print(maximum)









Output
89

Explanation

  • The lambda function compares two numbers.

  • It returns the larger one each time.

  • After all comparisons, the largest value remains.


Method 4 – Taking User Input

Find the sum of numbers entered by the user.

from functools import reduce numbers = list(map(int, input("Enter numbers separated by spaces: ").split())) result = reduce(lambda x, y: x + y, numbers) print("Sum:", result)













Sample Input
10 20 30 40

Output

Sum: 100

Explanation

  • input() reads the numbers as a string.

  • split() separates them into individual values.

  • map(int, ...) converts each value to an integer.

  • reduce() adds all the numbers and returns a single sum.


Comparison of Methods

MethodBest For
Normal FunctionReusable reduction logic
Lambda FunctionShort and simple operations
Finding MaximumComparing elements
User InputInteractive programs

๐Ÿ”ฅ Key Takeaways

  • reduce() is available in the functools module.

  • It applies a function repeatedly to reduce an iterable to a single value.

  • reduce() works with both normal functions and lambda functions.

  • It is commonly used for operations like sum, product, maximum, and minimum.

  • Unlike map() and filter(), reduce() returns a single result instead of an iterable.

Stay tuned for Day 99 of the #150DaysOfPython series! ๐Ÿš€

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (353) Android (25) AngularJS (1) API (2) Assembly Language (2) aws (31) Azure (12) BI (10) book (1) Books (359) Bootcamp (16) C (78) C# (12) C++ (83) cloud (1) Course (96) Coursera (306) Cybersecurity (36) data (10) Data Analysis (47) Data Analytics (31) data management (16) Data Science (438) Data Strucures (20) Deep Learning (223) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (9) Excel (24) Finance (13) flask (4) flutter (1) FPL (17) Gadgets (1) Generative AI (79) Git (14) Google (55) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (411) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (16) PHP (20) Projects (35) Python (1393) Python Coding Challenge (1282) Python Library (26) Python Mathematics (22) Python Mistakes (51) Python Pattern Challenge (29) python quixz (1) Python Quiz (665) Python Tips (114) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (21) SQL (55) Udemy (24) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)