Menu Close

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

Sometimes, we want to find the cumulative sum of numbers in a list with Python.

In this article, we’ll look at how to find the cumulative sum of numbers in a list with Python.

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

To find the cumulative sum of numbers in a list with Python, we can use the numpy cumsum method.

For instance, we write

import numpy as np

a = [4, 6, 12]

s = np.cumsum(a)

to call np.cumsum with list a to return the cumulative sum of the list items as a numpy array.

Conclusion

To find the cumulative sum of numbers in a list with Python, we can use the numpy cumsum method.

Posted in Python, Python Answers