- 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
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
When it is required to find the sum of a list where the specific element is sum of first few elements, a method is defined, that takes list as parameter. It uses list comprehension to find the cumulative sum.
Below is the demonstration of the same −
def cumulative_sum(my_list): cumulative_list =  my_length = len(my_list) cumulative_list = [sum(my_list[0:x:1]) for x in range(0, my_length+1)] return cumulative_list[1:] my_list = [10, 20, 25, 30, 40, 50] print("The list is :") print(my_list) print("The cumulative sum is :") print (cumulative_sum(my_list))
The list is : [10, 20, 25, 30, 40, 50] The cumulative sum is : [10, 30, 55, 85, 125, 175]
A method is defined, and a list is passed as a parameter to it.
An empty list is defined.
The length of the list is determined.
The list comprehension is used to iterate over the list.
It is converted to a list, and assigned to a variable.
The list from the second element to the last element is returned as output.
A list is defined outside the function and displayed on the console.
The method is called, and the list is passed as a parameter to it.
It is displayed as output on the console.
- Python program to find Cumulative sum of a list
- Find sum of frequency of given elements in the list in Python
- Program to find sum of the minimums of each sublist from a list in Python
- Program to find sum of minimum trees from the list of leaves in python
- Program to find sum of odd elements from list in Python
- Python program to find sum of elements in list
- How to find the sum of two list elements in R?
- Python Program to Find the Total Sum of a Nested List Using Recursion
- Find sum of elements in list in Python program
- Program to find the sum of the absolute differences of every pair in a sorted list in Python
- Python Program to find the cube of each list element
- Program to find out the number of submatrices from a matrix where the sum of elements is equal to a specific value in C++
- Program to check sublist sum is strictly greater than the total sum of given list Python
- C# Program to get the first three elements from a list