🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The functools.reduce function in Python's functools module applies a binary function cumulatively to the items of an iterable, from left to right, so as to reduce the iterable to a single value. This is useful for performing some computation on a list and returning the result.
Table of Contents
- Introduction
functools.reduceFunction Syntax- Examples
- Basic Usage
- Using with a Lambda Function
- Finding the Maximum Value
- Concatenating Strings
- Real-World Use Case
- Conclusion
Introduction
The functools.reduce function is used for performing cumulative operations on an iterable. It applies a specified binary function to the first two elements of the iterable, then to the result and the next element, and so on, until the iterable is reduced to a single value.
functools.reduce Function Syntax
Here is how you use the functools.reduce function:
import functools
result = functools.reduce(function, iterable[, initializer])
Parameters:
function: The binary function to apply. It takes two arguments.iterable: The iterable whose elements will be reduced.initializer: Optional. The initial value to start the accumulation. If not provided, the first item of the iterable is used as the initial value.
Returns:
- A single value obtained by cumulatively applying the function to the elements of the iterable.
Examples
Basic Usage
Sum the elements of a list.
Example
import functools
numbers = [1, 2, 3, 4, 5]
result = functools.reduce(lambda x, y: x + y, numbers)
print(result) # Output: 15
Using with a Lambda Function
Multiply the elements of a list.
Example
import functools
numbers = [1, 2, 3, 4, 5]
result = functools.reduce(lambda x, y: x * y, numbers)
print(result) # Output: 120
Finding the Maximum Value
Find the maximum value in a list.
Example
import functools
numbers = [5, 1, 8, 3, 2]
result = functools.reduce(lambda x, y: x if x > y else y, numbers)
print(result) # Output: 8
Concatenating Strings
Concatenate a list of strings into a single string.
Example
import functools
words = ["Hello", "world", "this", "is", "functools.reduce"]
result = functools.reduce(lambda x, y: x + " " + y, words)
print(result) # Output: Hello world this is functools.reduce
Real-World Use Case
Aggregating Data from a List of Dictionaries
Use reduce to aggregate data, such as summing the values of a specific key in a list of dictionaries.
Example
import functools
data = [
{"name": "Alice", "score": 10},
{"name": "Bob", "score": 20},
{"name": "Charlie", "score": 30}
]
total_score = functools.reduce(lambda x, y: x + y["score"], data, 0)
print(total_score) # Output: 60
Conclusion
The functools.reduce function is used for performing cumulative operations on an iterable. It can be used for various tasks such as summing, multiplying, finding the maximum value, concatenating strings, and more. Proper usage can simplify complex operations and enhance the readability of your code.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment