DEV Community

Cover image for How to use reduce function in Python
Ankit malik
Ankit malik

Posted on

How to use reduce function in Python

Introduction

In Python, the reduce function is a powerful tool for performing cumulative operations on iterable data, such as lists or tuples. Instead of writing a loop to repeatedly apply a function to elements, reduce lets you “reduce” the iterable into a single value by successively combining elements. This makes your code more concise and functional in style.

Syntax and Parameters

The reduce function is available in the functools module, so you must import it first:

from functools import reduce
reduce(function, iterable[, initializer])
Enter fullscreen mode Exit fullscreen mode

Parameters:

  • function: A function that takes two arguments and returns a single value.
  • iterable: The sequence (list, tuple, etc.) to process.
  • initializer (optional): A starting value. If provided, the reduction starts with this value; otherwise, the first item of the iterable is used.

Return value: A single value that is the cumulative result after applying the function across all elements.

Coding Examples

While reduce is often used with lambda functions but for learning purpose I am not going to user lambda function here.

Example 1: Summing Numbers

from functools import reduce

def add(x, y):
    return x + y

numbers = [2, 4, 6, 8]
result = reduce(add, numbers)
print(result)  # Output: 20
Enter fullscreen mode Exit fullscreen mode

Here, the add function is applied pairwise: ((2 + 4) + 6) + 8.

Example 2: Finding the Product of a List

from functools import reduce

def multiply(x, y):
    return x * y

numbers = [1, 3, 5, 7]
product = reduce(multiply, numbers)
print(product)  # Output: 105

Enter fullscreen mode Exit fullscreen mode

Example 3: Concatenating Strings

from functools import reduce

def join_strings(a, b):
    return a + " " + b

words = ["Python", "reduce", "function", "tutorial"]
sentence = reduce(join_strings, words)
print(sentence)  # Output: "Python reduce function tutorial"

Enter fullscreen mode Exit fullscreen mode

Example 4: Using an Initializer

from functools import reduce

def add(x, y):
    return x + y

numbers = [10, 20, 30]
result = reduce(add, numbers, 100)  # Starts with 100
print(result)  # Output: 160

Enter fullscreen mode Exit fullscreen mode

Real-World Use Cases

  • Data Processing: Summing sales figures, combining CSV rows, or aggregating sensor readings.
  • Mathematical Computations: Calculating factorials or cumulative products.
  • String Manipulation: Joining words or formatting complex strings.
  • Custom Aggregations: Reducing logs, merging dictionaries, or performing multi-step computations.

Best Practices

  • Prefer built-in functions like sum() or max() when possible—they are more readable and optimized.
  • Use named functions instead of lambdas for clarity, especially in complex operations.
  • Always import from functools, as reduce is not a built-in function in Python 3.
  • Provide an initializer to avoid errors when the iterable is empty.
  • Keep functions pure: Avoid side effects to ensure predictable results.

Conclusion

The reduce function in Python simplifies operations that require the combination of iterable elements into a single value. By understanding its syntax and applying it with clearly defined functions, you can write cleaner, more functional-style code. Whether you’re aggregating data, performing mathematical calculations, or manipulating strings, reduce is a valuable tool for elegant and concise Python programming.

Top comments (0)