Program to print the cumulative sum of the elements in the list using Python

Introduction

In this section of python programming, we will understand the python code to print the cumulative sum of the elements in the list. The ith element of the new list is the sum of (ith + 1) element.

Program to print the cumulative sum of the elements in the list

Program

input_list = [ 2, 2, 3, 5, 4, 3]
sum = 0
new_list = []
length = len(input_list)
upd_list = []
for i in range(length):
    sum = sum + input_list[i]
    upd_list.append(sum)
print("Cumulative list:",upd_list)

Output

Program to print the cumulative sum of the elements in the list Output

Explanation

In the above python code, we have created a list with elements. The for loop will iterate over the length of the list and calculates the arithmetic sum each time the value is traversed and appended to the updated list which we have created. When all the elements are traversed and the control comes out of for loop, the updated list of cumulative sum is printed on the screen

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.