- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a program to form a cumulative sum list in Python
The cumulative sum till ith element refers to the total sum from 0th to ith 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.
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]
- Python program to find Cumulative sum of a list
- Python – Cumulative Row Frequencies in 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
- Program to find minimum elements to add to form a given sum in Python
- Write a Golang program to convert a binary number to its decimal form
- Write a Golang program to convert a decimal number to its binary form
- How to create a cumulative sum plot in base R?
- 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
- Program to check whether list of points form a straight line or not in Python
- Python program to find the group sum till each K in a list
- Write a Golang program to calculate the sum of elements in a given array
- Program to encode a string in normal form to its run-length form in Python