GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||
GeezerVison is showing www.geeksforgeeks.org — reduce() in Python - GeeksforGeeks reduce() in PythonLast 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.
Output Geeks for Geeks Explanation:
Syntax
Parameters:
Returns: Returns a single final value after processing all elements. ExamplesExample 1: Here, the code adds all numbers in a list using reduce(). The function combines two numbers at a time.
Output 20 Explanation:
Example 2: Here, the code finds the largest number from a list using reduce().
Output 12 Explanation:
Example 3: This example uses functools.reduce() with built-in functions from operator module to perform sum, product and string concatenation on lists.
Output 17 180 geeksforgeeks Explanation:
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.
Output [1, 3, 6, 10, 15] Explanation: accumulate(a, add) - Adds elements cumulatively:
Let's understand the difference between accumulate() and reduce() more clearly with the help of the table below:
Comment Explore | ||||||||||||||||||
| 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. |