Supplementing the std lib (Was: partial sums problem)

kj at
Wed Sep 29 15:54:18 CEST 2010

In <mailman.1142.1285722789.29448.python-list at> Terry Reedy <tjreedy at> writes:

>Do not try to do a reduction with a comprehension. Just write clear, 
>straightforward code that obviously works.

>def cusum(s):
>   t = 0
>   for i in s:
>       t += i
>       yield t

> >>>
>[1, 3, 6, 10, 15, 21]

Actually, this is just fine.  Thank you!

But it brings up a new question, of an entirely different nature.

It's a recurrent conundrum, actually, and I have not found a good

Your cusum function is one that I would like to see somewhere in
the standard library (in itertools maybe?).  Maybe some future
version of the standard library will have it, or something like it
(I'm thinking of a generalized form which, like reduce, takes a
function and an initial value as arguments).

But in the immediate term, cusum is not part of the standard library.

Where would you put it if you wanted to reuse it?  Do you create
a module just for it?  Or do you create a general stdlib2 module
with all those workhorse functions that have not made it to the
standard library?  Or something else entirely?

(I'm not expecting to get *the* solution from anyone reply; rather,
I'm interested in reading people's take on the question and their
way of dealing with those functions they consider worthy of the
standard library.)


More information about the Python-list mailing list