GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask
  • Introduction1 min read
  • Input & Output2 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — reduce() in Python - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask

reduce() in Python

Last Updated : 22 Sep, 2026

reduce() function (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 result, current: result + " " + current, a)

print(r)

Output
Geeks for Geeks

Explanation:

  • result stores the value accumulated from the previous step.
  • current represents the next element from the list.
  • First, "Geeks" and "for" are combined to produce "Geeks for".

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 result, current: result + current, a)

print(r)

Output
20

Explanation:

  • result represents the sum calculated up to the current step.
  • current represents the next number from the list.
  • First, 2 + 4 gives 6.
  • Then, 6 + 6 gives 12.
  • Finally, 12 + 8 gives 20.
  • Therefore, the final result is 20.

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 result, current: result if result > current else current,
    a
)

print(r)

Output
12

Explanation:

  • lambda x, y: x if x > y else y compares two values at each step.
  • Keeps the larger one, resulting in the maximum value 12.

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()

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 the difference between accumulate() and reduce() more clearly with the help of the table below:

Featurereduce()accumulate()
Return ValueA single final value, such as 15.Intermediate results, such as [1, 3, 6, 10, 15].
Output TypeReturns a single value.Returns an iterator.
Use CaseUseful when only the final result is required.Useful when intermediate cumulative results are required.
ImportFrom functools.From itertools.
Argument OrderTakes the function first and the iterable second.Takes the iterable first and the function second.
Comment

Explore

    Python Fundamentals
    • Introduction1 min read
    • Input & Output2 min read
    • Variables4 min read
    • Operators4 min read
    • Keywords2 min read
    • Data Types4 min read
    • Conditional Statements3 min read
    • Loops3 min read
    • Functions4 min read
    Python Data Structures
    • String4 min read
    • List3 min read
    • Tuples3 min read
    • Dictionary3 min read
    • Set2 min read
    • Arrays5 min read
    Advanced Python
    • OOP Concepts3 min read
    • Exception Handling5 min read
    • File Handling4 min read
    • Python Database2 min read
    • MongoDB3 min read
    • MySQL7 min read
    • Packages4 min read
    • Modules3 min read
    • DSA Libraries5 min read
    • Python GUI2 min read
    Data Science with Python
    • Numpy3 min read
    • Pandas4 min read
    • Matplotlib3 min read
    • Seaborn3 min read
    • StatsModel2 min read
    • Model Building6 min read
    • TensorFlow2 min read
    • PyTorch5 min read
    Web Development with Python
    • Flask3 min read
    • Django5 min read
    • Django ORM4 min read
    • Jinja2 Templating5 min read
    • Django Templates5 min read
    • REST API3 min read
    • Build API with DRF4 min read
    Python Practice
    • Quiz1 min read
    • Practice Problems2 min read
    • Interview Q & A15+ min read
    Courses
    • Placement 360 Course2 min read
    • Data Science 360 Course2 min read
    • AI Engg Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.