Friday, 5 July 2024

Python — Using reduce()

Importing reduce
To use reduce(), you need to import it from the functools module:

from functools import reduce
Example 1: Sum of Elements
Let's start with a simple example: calculating the sum of all elements in a list.

numbers = [1, 2, 3, 4, 5]
result = reduce(lambda x, y: x + y, numbers)
print(result)  

#clcoding.com
15
Example 2: Product of Elements
Similarly, you can calculate the product of all elements in a list.

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

#clcoding.com
120
Example 3: Finding the Maximum Element
You can use reduce() to find the maximum element in a list.

numbers = [1, 2, 3, 4, 5]
result = reduce(lambda x, y: x if x > y else y, numbers)
print(result)  

#clcoding.com
5
Example 4: Concatenating Strings
You can use reduce() to concatenate a list of strings into a single string.

words = ["Hello", "World", "from", "Python"]
result = reduce(lambda x, y: x + " " + y, words)
print(result)  

#clcoding.com
Hello World from Python
Example 5: Using an Initial Value
You can provide an initial value to reduce(). This initial value is placed before the items of the sequence in the calculation and serves as a default when the sequence is empty.

numbers = [1, 2, 3, 4, 5]
result = reduce(lambda x, y: x + y, numbers, 10)
print(result)  

#clcoding.com
25
Example 6: Flattening a List of Lists
You can use reduce() to flatten a list of lists into a single list.

lists = [[1, 2, 3], [4, 5], [6, 7, 8]]
result = reduce(lambda x, y: x + y, lists)
print(result)  

#clcoding.com
[1, 2, 3, 4, 5, 6, 7, 8]
Example 7: Finding the Greatest Common Divisor (GCD)
You can use reduce() with the gcd function from the math module to find the GCD of a list of numbers.

import math

numbers = [48, 64, 256]
result = reduce(math.gcd, numbers)
print(result) 
16
Example 8: Combining Dictionaries
You can use reduce() to combine a list of dictionaries into a single dictionary.

dicts = [{'a': 1}, {'b': 2}, {'c': 3}]
result = reduce(lambda x, y: {**x, **y}, dicts)
print(result)  

#clcoding.com
{'a': 1, 'b': 2, 'c': 3}
Example 9: Custom Function with reduce()
You can also use a custom function with reduce(). Here's an example that calculates the sum of squares of elements in a list.

def sum_of_squares(x, y):
    return x + y**2

numbers = [1, 2, 3, 4]
result = reduce(sum_of_squares, numbers, 0)
print(result)  

#clcoding.com
30


0 Comments:

Post a Comment

Popular Posts

Categories

AI (33) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (146) C (77) C# (12) C++ (82) Course (67) Coursera (203) Cybersecurity (24) data management (11) Data Science (107) Data Strucures (8) Deep Learning (13) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (25) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (50) Meta (18) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (3) PHP (20) Projects (29) Python (897) Python Coding Challenge (285) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses