The problem statement asks to produce a new list whose i^{th} element will be equal to the sum of the (i + 1) elements.
Examples :
Input : list = [10, 20, 30, 40, 50] Output : [10, 30, 60, 100, 150] Input : list = [4, 10, 15, 18, 20] Output : [4, 14, 29, 47, 67]
Approach 1 :
We will use the concept of list comprehension and list slicing to get the cumulative sum of the list. The list comprehension has been used to access each element from the list and slicing has been done to access the elements from start to the i+1 element. We have used the sum() method to sum up the elements of the list from start to i+1.
Below is the implementation of the above approach :
Python3
# Python code to get the Cumulative sum of a list def Cumulative(lists): cu_list = [] length = len(lists) cu_list = [sum(lists[0:x:1]) for x in range(0, length+1)] return cu_list[1:]# Driver Code lists = [10, 20, 30, 40, 50] print (Cumulative(lists)) |
Output :
[10, 30, 60, 100, 150]
Approach 2:
Python3
list=[10,20,30,40,50]new_list=[] j=0for i in range(0,len(list)): j+=list[i] new_list.append(j) print(new_list) #code given by Divyanshu singh |
Output :
[10, 30, 60, 100, 150]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | Pandas Series.cumsum() to find cumulative sum of a Series
- Python - Cumulative List Split
- Cumulative sum of a column in Pandas - Python
- Python | Pandas series.cumprod() to find Cumulative product of a Series
- Python | Pandas series.cummax() to find Cumulative maximum of a series
- Python | Pandas Series.cummin() to find cumulative minimum of a series
- Python | CAP - Cumulative Accuracy Profile analysis
- Python - Cumulative Records Product
- Python - Cumulative product of dictionary value lists
- Python | Mathematical Median of Cumulative Records
- Cumulative percentage of a column in Pandas - Python
- Normalized Discounted Cumulative Gain - Multilabel Ranking Metrics | ML
- How to create a Cumulative Histogram in Plotly?
- Python | Ways to sum list of lists and return sum list
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
- Python | Convert List of String List to String List
- Python program to find sum of absolute difference between all pairs in a list
- Python program to find sum of elements in list
- Python program to find number of m contiguous elements of a List with a given sum
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

