MCPcopy Index your code
hub / github.com/python/cpython / reduce

Function reduce

Lib/functools.py:237–265  ·  view source on GitHub ↗

reduce(function, iterable, /[, initial]) -> value Apply a function of two arguments cumulatively to the items of an iterable, from left to right. This effectively reduces the iterable to a single value. If initial is present, it is placed before the items of the iterable in the c

(function, sequence, initial=_initial_missing)

Source from the content-addressed store, hash-verified

235_initial_missing = object()
236
237def reduce(function, sequence, initial=_initial_missing):
238 """
239 reduce(function, iterable, /[, initial]) -> value
240
241 Apply a function of two arguments cumulatively to the items of an iterable, from left to right.
242
243 This effectively reduces the iterable to a single value. If initial is present,
244 it is placed before the items of the iterable in the calculation, and serves as
245 a default when the iterable is empty.
246
247 For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
248 calculates ((((1 + 2) + 3) + 4) + 5).
249 """
250
251 it = iter(sequence)
252
253 if initial is _initial_missing:
254 try:
255 value = next(it)
256 except StopIteration:
257 raise TypeError(
258 "reduce() of empty iterable with no initial value") from None
259 else:
260 value = initial
261
262 for element in it:
263 value = function(value, element)
264
265 return value
266
267
268################################################################################

Callers 5

_sumFunction · 0.90
_ssFunction · 0.90
test_order_in_unionMethod · 0.90
prodFunction · 0.90
saveMethod · 0.85

Calls 1

functionFunction · 0.50

Tested by 2

test_order_in_unionMethod · 0.72
prodFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…