# Python program to find Cumulative sum of a list

## Python program to find Cumulative sum of a list

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 :

 # 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:

 list=[10,20,30,40,50] new_list=[] j=0 for 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]

Last Updated on March 17, 2022 by admin

## Python: os.path.abspath() method with examplePython: os.path.abspath() method with example

Python: os.path.abspath() method with example OS module in Python provides various methods for interacting with the

## Elon Musk Net Worth 2023: Salary, Net Worth in INR/Dollar & Income [Updated]Elon Musk Net Worth 2023: Salary, Net Worth in INR/Dollar & Income [Updated]

Elon Musk, a visionary entrepreneur and a driving force in advancing technology and space exploration,

## Generating all possible Subsequences using RecursionGenerating all possible Subsequences using Recursion

Generating all possible Subsequences using Recursion Given an array. The task is to generate and

## Pandas Series.str.strip(), lstrip() and rstrip()Pandas Series.str.strip(), lstrip() and rstrip()

Python | Pandas Series.str.strip(), lstrip() and rstrip() Python is a great language for doing data

## struct module in Pythonstruct module in Python

struct module in Python This module performs conversions between Python values and C structs represented

## Python statistics variance()Python statistics variance()

Python statistics | variance() Statistics module provides very powerful tools, which can be used to compute

## Get minimum values in rows or columns with their index position in Pandas-DataframeGet minimum values in rows or columns with their index position in Pandas-Dataframe

Get minimum values in rows or columns with their index position in Pandas-Dataframe Let’s discuss

## Classifying data using Support Vector Machines(SVMs) in PythonClassifying data using Support Vector Machines(SVMs) in Python

Classifying data using Support Vector Machines(SVMs) in Python Introduction to SVMs: In machine learning, support