Q:

Python program to find the cumulative sum of elements of a list

belongs to collection: Python List Programs

0

Cumulative Sum is the sequence of partial sum i.e. the sum till current index.

Program to find the cumulative sum of elements of the list,

In this problem, we will take the list as input from the user. Then print the cumulative sum of elements of the list.

Example:

Input:
[1, 7, 3, 5, 2, 9]

Output:
[1, 8, 11, 16, 18, 27]

To perform the task, we need to iterate over the list and find the sum till the current index and, store it to a list and then print it.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to find the cumulative sum of elements in a list

# Python program to find cumulative sum 
# of elements of list

# Getting list from user
myList = []
length = int(input("Enter number of elements : "))
for i in range(0, length):
    value = int(input())
    myList.append(value)

# finding cumulative sum of elements
cumList = []
sumVal = 0
for x in myList:
    sumVal += x
    cumList.append(sumVal) 

# Printing lists 
print("Entered List ", myList)
print("Cumulative sum List ", cumList)

Output:

Enter number of elements : 5
1
7
3
5
2
Entered List  [1, 7, 3, 5, 2]
Cumulative sum List  [1, 8, 11, 16, 18]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python List Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to clone or copy a list in Python? Cloning or ... >>
<< Python program to interchange first and last eleme...