Categories
Python Answers

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

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *