reduce() in Python

Last Updated : 17 Feb, 2026

The reduce() function in Python (from the functools module) applies a function cumulatively to the elements of an iterable and returns a single final value. It processes elements step-by-step, combining two elements at a time until only one result remains.

Example: In this example, reduce() combines all strings in a list into one sentence.

Python
from functools import reduce
a = ["Geeks", "for", "Geeks"]
r = reduce(lambda x, y: x + " " + y, a)
print(r)

Output
Geeks for Geeks

Explanation: reduce(lambda x, y: x + " " + y, a) joins elements step-by-step into one string.

Syntax

reduce(function, iterable[, initializer])

Parameters:

  • function: A function that takes two arguments and returns a single value.
  • iterable: The sequence to be reduced (list, tuple, etc.).
  • initializer (optional): A starting value that is placed before first element.

Returns: Returns a single final value after processing all elements.

Examples

Example 1: Here, the code adds all numbers in a list using reduce(). The function combines two numbers at a time.

Python
from functools import reduce
a = [2, 4, 6, 8]
r = reduce(lambda x, y: x + y, a)
print(r)

Output
20

Explanation: reduce(lambda x, y: x + y, a) adds elements step-by-step (((1+2)+3)+4)+5 --> 15.

Example 2: Here, the code finds the largest number from a list using reduce().

Python
from functools import reduce
a = [5, 9, 3, 12, 7]
r = reduce(lambda x, y: x if x > y else y, a)
print(r)

Output
12

Explanation: lambda x, y: x if x > y else y keeps the larger value each step --> (((1*2)*3)*4)*5 = 120.

Example 3: This example uses functools.reduce() with built-in functions from operator module to perform sum, product and string concatenation on lists.

Python
import functools
import operator
a = [1, 3, 5, 6, 2]

print(functools.reduce(operator.add, a))
print(functools.reduce(operator.mul, a)) 
print(functools.reduce(operator.add, ["geeks", "for", "geeks"]))

Output
17
180
geeksforgeeks

Explanation:

  • functools.reduce(operator.add, a): Adds all numbers in the list - 1+3+5+6+2 = 17.
  • functools.reduce(operator.mul, a): Multiplies all numbers in the list - 1*3*5*6*2 = 180.
  • functools.reduce(operator.add, ["geeks", "for", "geeks"]): Concatenates all strings in list - "geeksforgeeks"

Difference Between reduce() and accumulate()

The accumulate() function (from itertools) and reduce() both apply a function cumulatively to items in a sequence. However, accumulate() returns an iterator of intermediate results, while reduce() returns only final value.

Example: This code demonstrates how accumulate() from itertools module works it performs cumulative operations and returns all intermediate results instead of just a single final value.

Python
from itertools import accumulate
from operator import add

a = [1, 2, 3, 4, 5]
res = accumulate(a, add)
print(list(res))

Output
[1, 3, 6, 10, 15]

Explanation: accumulate(a, add) - Adds elements cumulatively:

  • Step 1: 1
  • Step 2: 1 + 2 = 3
  • Step 3: 3 + 3 = 6
  • Step 4: 6 + 4 = 10
  • Step 5: 10 + 5 = 15

Let's understand difference between accumulate() and reduce() more clearly with the help of below table:

Featurereduce()accumulate()
Return ValueA single final value (e.g., 15).Intermediate results (e.g., [1, 3, 6, 10, 15]).
Output TypeReturns a single value.Returns an iterator.
Use CaseUseful when only the final result is needed.Useful when tracking cumulative steps.
ImportFrom functools.From itertools.
Suggested Quiz

0 Questions

Quiz Completed Successfully

Your Score : 0/0

Accuracy : 0%

Comment
Article Tags:

Explore