Fundamentals 6 min read

Using Python's reduce() Function: Basics and Practical Examples

This article introduces Python's reduce() function from the functools module, explains its syntax and parameters, and provides ten detailed examples ranging from summing numbers and concatenating strings to computing GCD, products, maximums, minimums, and longest string lengths.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Using Python's reduce() Function: Basics and Practical Examples

Introduction

In Python, the reduce() function is a useful tool for aggregating elements of a sequence using a binary function. It belongs to the functools module, so you must import it before use.

1. Basic Usage

The basic form is:

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

where function is a binary function, iterable is any iterable (list, tuple, etc.), and initializer is optional for initializing the accumulator.

2. Example: Sum of List Elements

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

3. Example: Sum of String Lengths

from functools import reduce
def add_length(x, y):
    return x + len(y)
strings = ["hello", "world", "python"]
result = reduce(add_length, strings, 0)
print(result)  # 输出:18

4. Example: Greatest Common Divisor

from functools import reduce
import math
def gcd(x, y):
    return math.gcd(x, y)
numbers = [12, 24, 36, 48]
result = reduce(gcd, numbers)
print(result)  # 输出:12

5. Example: Product of List Elements

from functools import reduce
def multiply(x, y):
    return x * y
numbers = [1, 2, 3, 4, 5]
result = reduce(multiply, numbers)
print(result)  # 输出:120

6. Example: Concatenate Strings

from functools import reduce
def concatenate_strings(x, y):
    return x + y
strings = ["hello", " ", "world", " ", "python"]
result = reduce(concatenate_strings, strings)
print(result)  # 输出:'hello world python'

7. Example: Accumulate Sum with Initializer

from functools import reduce
def accumulate_sum(x, y):
    return x + y
numbers = [1, 2, 3, 4, 5]
result = reduce(accumulate_sum, numbers, 0)
print(result)  # 输出:15

8. Example: Maximum Value

from functools import reduce
def max_value(x, y):
    return x if x > y else y
numbers = [1, 2, 3, 4, 5]
result = reduce(max_value, numbers)
print(result)  # 输出:5

9. Example: Minimum Value

from functools import reduce
def min_value(x, y):
    return x if x < y else y
numbers = [1, 2, 3, 4, 5]
result = reduce(min_value, numbers)
print(result)  # 输出:1

10. Example: Longest String Length

from functools import reduce
def max_length(x, y):
    return max(len(x), len(y))
strings = ["hello", "world", "python"]
result = reduce(max_length, strings)
print(result)  # 输出:6

Summary

The examples demonstrate how reduce() can be applied to compute sums, products, GCD, maximum/minimum values, concatenate strings, and calculate lengths, illustrating its versatility for aggregating data in Python.

PythonFunctional ProgrammingTutorialreduceexamples
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.