Supplementing the std lib (Was: partial sums problem)

Ian Kelly ian.g.kelly at
Thu Sep 30 04:17:46 CEST 2010

On Wed, Sep 29, 2010 at 7:06 PM, Paul Rubin < at nospam.invalid> wrote:

> As for the stdlib, the natural places for such a function would be
> either itertools or functools, and the function should probably be called
> "scan", inspired by this:
> Python's version would be like "scanl" with an optional arg to make it
> like "scanl1".

Something like this?

NoInitialValue = object()

def scan(function, iterable, initial=NoInitialValue):
    iterator = iter(iterable)

    if initial is NoInitialValue:
            accumulator =
        except StopIteration:
        accumulator = initial

    yield accumulator

    for arg in iterator:
        accumulator = function(accumulator, arg)
        yield accumulator

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list