Python - Incremental Sublist Sum


In Incrementak Sublist sum, there will be a given list that contains some integer element and that allows for some specific condition and iteration to add the preceding element. In Python, we have some built−in functions such as accumulate(), cumsum(), and tolist(), that will be used to solve the Incremental Sublist Sum.

Let’s take an example of this.

The given list, [10, 20, 30, 40, 50]

10 : 0th Index

10 + 20 = 30 : 1st Index

10+ 20 + 30 = 60 : 2nd Index

10 + 20 + 30 + 40 = 100 : 3rd Index

10 + 20 + 30 + 40 + 50 = 150 : 4th Index

Therefore, the final result becomes [10, 30, 60, 100, 150]

Syntax

The following syntax is used in the examples-

sum()

The sum() is an in−built in Python that allows adding of the values in an iterable form.

range()

The range() is an in−built function in Python that returns the sequence of numbers according to a given range.

len()

The len() is an in−built function in Python that returns the length of the result.

accumulate()

The accumulate() is an in−built function in Python that returns the addition of an iterable element of the given list.

cumsum()

The cumsum() is an in−built function in Python that follows the numpy module to add the iterable element of the given list. This function is mostly used in Python Dataframe with the cumulative sum of each row.

tolist()

The tolist() is a built−in function in Python that returns the series of lists.

Using a loop

In the following example, the program uses a recursive function that calls itself. Then use the empty list in the variable result which will store the final output of the program during function return. Next, initialize the initial value of current_sum to 0 that will be used to set the first index element. Then using for loop, iterates over the input list by using += and append() to calculate the incremental Sublist Sum. Finally, use the function return to get the result.

Example

def Inc_sublist_sum(lst):
    result = []
    current_sum = 0
    for num in lst:
        current_sum += num
        result.append(current_sum)
    return result
lst = [100, 50, 120, 60, 30, 40]
increment_sum = Inc_sublist_sum(lst)
print(increment_sum)

Output

 [100, 150, 270, 330, 360, 400]

Using list Comprehension

In the following example, there will be using list comprehension where built−in function sum() set the slicing notation to add the incremental sublist sum. Using for loop it iterates over the length of the list and returns the result.

Example

def Inc_sublist_sum(lst):
    return [sum(lst[:i+1]) for i in range(len(lst))]
# Create the list
lst = [78, 10, 88, 20]
increment_sum = Inc_sublist_sum(lst)
print(increment_sum)

Output

 [78, 88, 176, 196]

Using Accumulate from Itertools

In the following example, we will start the program by defining the library itertools and importing the module named accumulate. Next, it will use the recursive function that provides the Python built−in function accumulate() which is automatically added for the iterable element and using list() it returns the output of the program.

Example

from itertools import accumulate
# recursive function
def Inc_sublist_sum(lst):
    return list(accumulate(lst))
# create the list
lst = [23, 43, 10]
# calling function
res = Inc_sublist_sum(lst)
print(res)

Output

 [23, 66, 76]

Using Numpy

In the following example, we will start the program by importing the module named numpy and take the object reference as np. Then it will use the recursive function where it set the built−in function cumsum() and tolist() to calculate the Incremental Sublist Sum and return the result.

Example

import numpy as np
# Recursive function
def Inc_sublist_sum(lst):
    return np.cumsum(lst).tolist()
# Create the list
lst = [11, 21, 31, 41, 51]
increment_sum = Inc_sublist_sum(lst)
print(increment_sum)

Output

 [11, 32, 63, 104, 155]

Conclusion

All the above examples represent the Incremental Sublists Sum where some iterable built−in function used such as accumulate(), sum(), cumsum(), and, tolist(). The best possible way to understand this example is by using simple += operator which helps to add the iterable element of list.

Updated on: 14-Aug-2023

59 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements