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.
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) # 输出:153. 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) # 输出:184. 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) # 输出:125. 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) # 输出:1206. 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) # 输出:158. 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) # 输出:59. 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) # 输出:110. 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) # 输出:6Summary
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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
