Open In App

reduce() in Python

Last Updated : 14 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The reduce() function in Python applies a given function cumulatively to all items in an iterable, reducing it to a single final value.

It’s a method of the functools module, so we need to import it before use:

Syntax

from functools import reduce
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.

Return Value: A single final value after processing all elements.

Examples

Let's look at some examples of reduce() function.

Example 1: Basic Usage with a Named Function

This code uses reduce() function to accumulate values in a list by repeatedly adding two numbers at a time.

Python
from functools import reduce

# Function to add two numbers
def add(x, y):
    return x + y

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

Output
15

Explanation:

  • def add(x, y): return x + y - Defines a function that returns sum of two numbers.
  • reduce(add, a) - Applies add cumulatively to the list (((1+2)+3)+4)+5 --> 15.

Example 2: Using reduce() with a Lambda Function

Here, lambda function is used with reduce() to sum all the numbers in a list.

Python
from functools import reduce
a = [1, 2, 3, 4, 5]
res = reduce(lambda x, y: x + y, a)
print(res)

Output
15

Explanation: reduce(lambda x, y: x + y, a) repeatedly adds two numbers at a time from list (((1+2)+3)+4)+5 --> 15

Example 3: Using reduce() with operator Module

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)) # Sum of list
print(functools.reduce(operator.mul, a)) # Product of list
print(functools.reduce(operator.add, ["geeks", "for", "geeks"])) # String concatenation

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"

Example 4: Using initializer

This code uses reduce() with a lambda function and an initial value to sum a list, starting from a given number.

Python
from functools import reduce
a = [1, 2, 3]
res = reduce(lambda x, y: x + y, a, 10)
print(res)

Output
16

Explanation: reduce(lambda x, y: x + y, a, 10) starts with 10 as initial value, then adds each element in the list ((10+1)+2)+3 = 16.

Difference Between reduce() and accumulate()

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

Let's understand it better with an example.

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.

Common Use Cases of reduce()

  1. Summing all numbers in a list.
  2. Multiplying elements for a factorial.
  3. Finding maximum or minimum in a sequence.
  4. Concatenating strings.
  5. Flattening nested lists.

When to Use and When to Avoid

Use when:

  • You need to combine items into a single value.
  • The logic is short and simple.

Avoid when:

  • The function logic is complex (use loops for readability).
  • You need intermediate results (use accumulate() instead).
Suggested Quiz
6 Questions

What is the primary purpose of the reduce() function in Python?

  • A

    To reduce the size of a list

  • B

    To apply a function to items in a list and return a single cumulative result

  • C

    To remove duplicates from a list

  • D

    To filter elements from a list based on a condition

Explanation:

reduce() repeatedly applies a function to the items of an iterable to reduce it to a single value.

Which module must be imported to use reduce() in Python 3?

  • A

    math

  • B

    operator

  • C

    functools

  • D

    itertools

Explanation:


What is the output of the following code?


from functools import reduce

nums = [1, 2, 3, 4]

result = reduce(lambda x, y: x + y, nums)

print(result)

  • A

    10

  • B

    24

  • C

    1234

  • D

    Error

Explanation:

reduce() applies the lambda cumulatively: (((1+2)+3)+4) = 10.

How does the reduce function differ from the map function in Python?

  • A

    Reduce returns a single cumulative value, while map returns an iterable of results

  • B

    Reduce can only work with numeric types, while map can work with any type

  • C

    Reduce applies the function to each element independently, while map combines them

  • D

    Reduce requires an initializer, while map does not

Explanation:


What will this code return?


from functools import reduce


nums = [2, 3, 4]

result = reduce(lambda x, y: x * y, nums)

print(result)

  • A

    24

  • B

    9

  • C

    Error

  • D

    6

Explanation:

Multiplying cumulatively: 2 * 3 = 6, then 6 * 4 = 24.

Which of the following is not true about reduce()?

  • A

    It can only be used with numeric data types

  • B

    It stops execution if the iterable is empty and no initializer is provided

  • C

    It returns a single value after processing all elements

  • D

    It accepts an optional initializer argument

Explanation:

reduce() can be used with any data types, such as strings, as long as the function supports them.

Quiz Completed Successfully
Your Score :   2/6
Accuracy :  0%
Login to View Explanation
1/6 1/6 < Previous Next >

Article Tags :
Practice Tags :

Similar Reads