How to find the cumulative sum of numbers in a list? How to find the cumulative sum of numbers in a list? python python

How to find the cumulative sum of numbers in a list?


If you're doing much numerical work with arrays like this, I'd suggest numpy, which comes with a cumulative sum function cumsum:

import numpy as npa = [4,6,12]np.cumsum(a)#array([4, 10, 22])

Numpy is often faster than pure python for this kind of thing, see in comparison to @Ashwini's accumu:

In [136]: timeit list(accumu(range(1000)))10000 loops, best of 3: 161 us per loopIn [137]: timeit list(accumu(xrange(1000)))10000 loops, best of 3: 147 us per loopIn [138]: timeit np.cumsum(np.arange(1000))100000 loops, best of 3: 10.1 us per loop

But of course if it's the only place you'll use numpy, it might not be worth having a dependence on it.


In Python 2 you can define your own generator function like this:

def accumu(lis):    total = 0    for x in lis:        total += x        yield totalIn [4]: list(accumu([4,6,12]))Out[4]: [4, 10, 22]

And in Python 3.2+ you can use itertools.accumulate():

In [1]: lis = [4,6,12]In [2]: from itertools import accumulateIn [3]: list(accumulate(lis))Out[3]: [4, 10, 22]


I did a bench-mark of the top two answers with Python 3.4 and I found itertools.accumulate is faster than numpy.cumsum under many circumstances, often much faster. However, as you can see from the comments, this may not always be the case, and it's difficult to exhaustively explore all options. (Feel free to add a comment or edit this post if you have further benchmark results of interest.)

Some timings...

For short lists accumulate is about 4 times faster:

from timeit import timeitdef sum1(l):    from itertools import accumulate    return list(accumulate(l))def sum2(l):    from numpy import cumsum    return list(cumsum(l))l = [1, 2, 3, 4, 5]timeit(lambda: sum1(l), number=100000)# 0.4243644131347537timeit(lambda: sum2(l), number=100000)# 1.7077815784141421

For longer lists accumulate is about 3 times faster:

l = [1, 2, 3, 4, 5]*1000timeit(lambda: sum1(l), number=100000)# 19.174508565105498timeit(lambda: sum2(l), number=100000)# 61.871223849244416

If the numpy array is not cast to list, accumulate is still about 2 times faster:

from timeit import timeitdef sum1(l):    from itertools import accumulate    return list(accumulate(l))def sum2(l):    from numpy import cumsum    return cumsum(l)l = [1, 2, 3, 4, 5]*1000print(timeit(lambda: sum1(l), number=100000))# 19.18597290944308print(timeit(lambda: sum2(l), number=100000))# 37.759664884768426

If you put the imports outside of the two functions and still return a numpy array, accumulate is still nearly 2 times faster:

from timeit import timeitfrom itertools import accumulatefrom numpy import cumsumdef sum1(l):    return list(accumulate(l))def sum2(l):    return cumsum(l)l = [1, 2, 3, 4, 5]*1000timeit(lambda: sum1(l), number=100000)# 19.042188624851406timeit(lambda: sum2(l), number=100000)# 35.17324400227517