惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Martin Fowler
Martin Fowler
Y
Y Combinator Blog
M
MIT News - Artificial intelligence
The Cloudflare Blog
WordPress大学
WordPress大学
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 司徒正美
小众软件
小众软件
Blog — PlanetScale
Blog — PlanetScale
雷峰网
雷峰网
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
J
Java Code Geeks
云风的 BLOG
云风的 BLOG
C
Check Point Blog
D
DataBreaches.Net
T
The Blog of Author Tim Ferriss
V
V2EX
F
Fortinet All Blogs
B
Blog
大猫的无限游戏
大猫的无限游戏
N
Netflix TechBlog - Medium
B
Blog RSS Feed
A
About on SuperTechFans
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC

博客园 - 漫思

python使用.env构建开发和生产环境 python项目的构建 nodejs构建CICD时的思考  成为 AI 智能体工程师的 10 个步骤 es6的 yield python 中的 yield 笔记本的A壳 thinkpad 更换 python的字段赋值和取值的操作 python的语法类似于lodash分组展开,合并分组操作 SelectMany C# lodash 数组的常用做法 lodash里面的常用方法 技术的边界 Reduce 和 Transduce 的含义 尤雨溪创办的 VoidZero 官宣加入 Cloudflare,前端 Vite 等保持开源 Ramda 函数库参考教程 ramda es 数组的方法 flatmap map object.entity的教程 Map与FlatMap:在数据处理中的区别与联系 flat、flatmap与map的用法区别1 flat、flatmap与map的用法区别 AI不是从天而降,它经历了七十年三起三落:读懂AI的第三课 Agent 17 种架构模式 分析 & 思考 只有踩过坑才懂:前端生成唯一 ID,别用 Date.now ()了!试试它crypto.randomUUID() FastAPI python并发 代码是 AI 写的,生产事故谁背锅? AI Agent 走出 Demo 幻觉的唯一解药:Harness Engineering 高管的 AI 精神病
reduce() in Python
漫思 · 2026-06-10 · via 博客园 - 漫思

reduce() in Python

Last Updated : 9 Jun, 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.

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.

Explanation: reduce(lambda x, y: x + y, a) adds elements step-by-step ((2 + 4) + 6) + 8 = 20.

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

Explanation: lambda x, y: x if x > y else y compares two values at each step and 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.

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.

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 Value A single final value (e.g., 15). Intermediate results (e.g., [1, 3, 6, 10, 15]).
Output Type Returns a single value. Returns an iterator.
Use Case Useful when only the final result is needed. Useful when tracking cumulative steps.
Import From functools. From itertools.

漫思

posted on 2026-06-10 20:02  漫思  阅读(4)  评论()    收藏  举报