reduce(function, sequence) returns a single value constructed
by calling the (binary) function on the first two items of the
sequence, then on the result and the next item, and so on. For
example, to compute the sum of the numbers 1 through 10:
>>> reduce(lambda x, y: x+y, range(1, 11)) 55 >>>
If there's only one item in the sequence, its value is returned; if the sequence is empty, an exception is raised.
A third argument can be passed to indicate the starting value. In this case the starting value is returned for an empty sequence, and the function is first applied to the starting value and the first sequence item, then to the result and the next item, and so on. For example,
>>> def sum(seq): ... return reduce(lambda x, y: x+y, seq, 0) ... >>> sum(range(1, 11)) 55 >>> sum() 0 >>>