- Related Questions & Answers
- Python program to find Cumulative sum of a list
- How to create a Cumulative Sum Column in MySQL?
- Python Program to Find the Cumulative Sum of a List where the ith Element is the Sum of the First i+1 Elements From The Original List
- How to create a cumulative sum plot in base R?
- Write a Golang program to convert a decimal number to its binary form
- Write a Golang program to convert a binary number to its decimal form
- Write a C program to display all datatypes ranges in tabular form
- Program to merge two sorted list to form larger sorted list in Python
- Cumulative sum of elements in JavaScript
- Program to check whether list of points form a straight line or not in Python
- Python program to find sum of elements in list
- Program to encode a string in normal form to its run-length form in Python
- Program to find sum of non-adjacent elements in a circular list in python
- Write a program in Python to print dataframe rows as orderDict with a list of tuple values
- How to create a column in an R data frame with cumulative sum?

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

The cumulative sum till i^{th} element refers to the total sum from 0^{th} to i^{th} element.

The program statement is to form a new list from a given list. The ith element in the new list will be the cumulative sum from 0 to ith element in the given list.

For example,

[10,20,30,40,50]

[10,30,60,100,150]

[1,2,3,4,5]

[1,3,6,10,15]

Following is a program to form a cumulative sum list using the input list −

The input list is passed to the function cumSum() which returns the cumulative sum list.

We declare an empty list cum_list to which we will append elements to form the cumulative sum list.

Initialize a sum variable sm=0.

Start iterating over the input list, with each iteration we increment the sum value to previous value+ the current element.

On each iteration, the sum value is appended to the cum_list.

Thus, on ith iteration, the sum variable will contain sum till the ith element(included), which is then appended to the cum_list.

After iterating through the whole list, the cum_list is returned.

def cumSum(s): sm=0 cum_list=[] for i in s: sm=sm+i cum_list.append(sm) return cum_list a=[10,20,30,40,50] print(cumSum(a))

[10, 30, 60, 100, 150]

Advertisements