Open In App

Python program to find Cumulative sum of a list

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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]

Time Complexity: O(n)
Auxiliary Space: O(n)

Approach 2:
 

Python3




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]

Time Complexity: O(n)

Auxiliary Space: O(n)

Alternate approach : Use itertools module

 One approach that is not mentioned in the provided solution is to use the built-in accumulate() function from the itertools module. This function allows you to perform a cumulative sum of the elements in an iterable, and returns an iterator that produces the cumulative sum at each step.

To use this function, you can pass your list as the first argument, and specify the operator.add function as the second argument, which will be used to perform the cumulative sum. Here is an example of how this can be implemented:

Python3




from itertools import accumulate
import operator
 
def cumulative_sum(input_list):
    # Use the accumulate() function to perform a cumulative sum of the elements in the list
    cumulative_sum_iter = accumulate(input_list, operator.add)
    # Convert the iterator to a list and return it
    return list(cumulative_sum_iter)
 
input_list = [10, 20, 30, 40, 50]
output_list = cumulative_sum(input_list)
print(output_list)


Output

[10, 30, 60, 100, 150]

Approach: using numpy.cumsum()

note: install numpy module using command “pip install numpy”

NumPy has many mathematical functions one of them is the cumsum() function. This function returns the cumulative sum of the elements in an array along a specified axis.

To use this function, we first need to convert our list into a NumPy array using the numpy.array() function. We can then call the cumsum() function on the resulting array to compute the cumulative sum. Finally, we can convert the resulting NumPy array back into a Python list using the tolist() method.

Python3




import numpy as np
 
def cumulative_sum(input_list):
    # Convert the list to a NumPy array
    input_array = np.array(input_list)
    # Compute the cumulative sum along the first axis of the array
    cumulative_sum_array = np.cumsum(input_array)
    # Convert the NumPy array back to a list and return it
    return cumulative_sum_array.tolist()
 
input_list = [10, 20, 30, 40, 50]
output_list = cumulative_sum(input_list)
print(output_list)


Output:
[10, 30, 60, 100, 150]

Algorithm Analysis:
The time complexity of the cumulative_sum() function is O(n), where n is the length of the input list because it involves only one pass over the input list to compute the cumulative sum. The space complexity of the function is also O(n) because it creates a NumPy array of size n to store the cumulative sum.

METHOD 5:Using counter method

APPROACH:

The program finds the cumulative sum of a list using the Counter method from the collections module in Python.

ALGORITHM:

1.Create a Counter object from the input list to get the count of each element.
2.Initialize an empty list called cum_sum to store the cumulative sum.
3.Append the first element of the input list to the cum_sum list.
4.Loop over the remaining elements of the input list and compute the cumulative sum by adding the current element to the previous element in the cum_sum list.
5.Append the cumulative sum to the cum_sum list.
6.Return the cum_sum list.

Python3




from collections import Counter
 
def cumulative_sum(lst):
    cnt = Counter(lst)
    cum_sum = [lst[0]]
    for i in range(1, len(lst)):
        cum_sum.append(cum_sum[i-1] + lst[i])
    return cum_sum
 
lst = [10, 20, 30, 40, 50]
result = cumulative_sum(lst)
print(result)


Output

[10, 30, 60, 100, 150]

Time complexity:

The time complexity of the program is O(n), where n is the length of the input list. The program has to loop over the input list once to compute the cumulative sum.

Space complexity:

The space complexity of the program is O(n), where n is the length of the input list. The program uses an additional list to store the cumulative sum of the input list.



Last Updated : 03 Jul, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads