The problem statement asks to produce a new list whose element will be equal to the sum of the elements.
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]
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 implementation of above approach :
[10, 30, 60, 100, 150]
- 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 | Pandas Series.cumsum() to find cumulative sum of a Series
- Python program to find sum of elements in list
- Python program to find second largest number in a list
- Python program to find smallest number in a list
- Python program to find largest number in a list
- Python program to find N largest elements from a list
- Python program to find middle of a linked list using one traversal
- Python program to find sum of absolute difference between all pairs in a list
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Find maximum length sub-list in a nested list
- Python program to right rotate a list by n
- Python program to interchange first and last elements in a list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.