def reduce(function, sequence, initial=_initial_missing):
"""
- reduce(function, sequence[, initial]) -> value
+ reduce(function, iterable[, initial]) -> value
- Apply a function of two arguments cumulatively to the items of a sequence,
- from left to right, so as to reduce the sequence to a single value.
- For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
+ Apply a function of two arguments cumulatively to the items of a sequence
+ or iterable, from left to right, so as to reduce the iterable to a single
+ value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
((((1+2)+3)+4)+5). If initial is present, it is placed before the items
- of the sequence in the calculation, and serves as a default when the
- sequence is empty.
+ of the iterable in the calculation, and serves as a default when the
+ iterable is empty.
"""
it = iter(sequence)
try:
value = next(it)
except StopIteration:
- raise TypeError("reduce() of empty sequence with no initial value") from None
+ raise TypeError(
+ "reduce() of empty iterable with no initial value") from None
else:
value = initial
if (result == NULL)
PyErr_SetString(PyExc_TypeError,
- "reduce() of empty sequence with no initial value");
+ "reduce() of empty iterable with no initial value");
Py_DECREF(it);
return result;
}
PyDoc_STRVAR(functools_reduce_doc,
-"reduce(function, sequence[, initial]) -> value\n\
+"reduce(function, iterable[, initial]) -> value\n\
\n\
-Apply a function of two arguments cumulatively to the items of a sequence,\n\
-from left to right, so as to reduce the sequence to a single value.\n\
-For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
+Apply a function of two arguments cumulatively to the items of a sequence\n\
+or iterable, from left to right, so as to reduce the iterable to a single\n\
+value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n\
((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n\
-of the sequence in the calculation, and serves as a default when the\n\
-sequence is empty.");
+of the iterable in the calculation, and serves as a default when the\n\
+iterable is empty.");
/* lru_cache object **********************************************************/