generate list of partially accumulated values

Paul Rubin http
Sun Sep 16 12:25:10 EDT 2007


cesco <fd.calabrese at gmail.com> writes:
> I have the following list:
> l = [1, 2, 3, 4]
> and I'd like to obtain a list like the following:
> l_partial_sum = [1, 3, 6, 10] (that is [1, 1+2, 1+2+3, 1+2+3+4])
> 
> Is there a simple way to accomplish this?

I won't show explicit code since you are apparently asking a homework
question.  But Python style tends to prefer doing this sort of thing
imperatively, possibly using generator functions.



More information about the Python-list mailing list