Friday, September 19, 2025
HomeLanguagesPython – Incremental Sublist Sum

Python – Incremental Sublist Sum

Sometimes we need to group elements and grouping techniques and requirements vary accordingly. One such way to group the elements is by the i’th size in list which stores the dictionary of index keys with values of summation of subsequent size i. Let’s discuss certain ways in which this can be done. 

Method #1 : Using islice() + sum() + dictionary comprehension The slice method can be used to group the chunks of list required to be made as values of the dictionaries which are then assigned to their destined index key using the dictionary comprehension. The sum() is used to perform sum of created list. 

Python3




# Python3 code to demonstrate
# Incremental Sublist Sum
# using islice() + sum() + dictionary comprehension
from itertools import islice
 
# initializing list
test_list = [4, 7, 8, 10, 12, 15, 13, 17, 14, 5]
 
# printing original list
print("The original list : " + str(test_list))
 
# using islice() + sum() + dictionary comprehension
# Incremental Sublist Sum
temp = iter(test_list)
res = {key: val for key, val in ((i, sum(list(islice(temp, i)))) for i in range(1, len(test_list))) if val}
 
# printing result
print("The Incremental Sublist Sum is : " + str(res))


Output : 

The original list : [4, 7, 8, 10, 12, 15, 13, 17, 14, 5]
The Incremental Sublist Sum is : {1: 4, 2: 15, 3: 37, 4: 49}

Time complexity: O(n^2) because it uses a nested loop to iterate over all possible sublists.

Auxiliary space: O(n^2) because it creates a dictionary res that potentially stores all possible sublists of the input list test_list.

  Method #2 : Using itemgetter() + takewhile() + islice() + sum() In order to increase the computation speed, we introduce new functions to perform this particular task, takewhile and itemgetter functions which performs the task of grouping the sliced values. The sum() is used to perform sum of created list. 

Python3




# Python3 code to demonstrate
# Incremental Sublist Sum
# using itemgetter() + takewhile() + islice() + sum()
from itertools import islice, takewhile
from operator import itemgetter
 
# initializing list
test_list = [4, 7, 8, 10, 12, 15, 13, 17, 14, 5]
 
# printing original list
print("The original list : " + str(test_list))
 
# using itemgetter() + takewhile() + islice() + sum()
# Incremental Sublist Sum
temp = iter(test_list)
res = {key: val for key, val in takewhile(itemgetter(1), ((i, sum(list(islice(temp, i)))) for i in range(1, len(test_list))))}
 
# printing result
print("The Incremental Sublist Sum is : " + str(res))


Output : 

The original list : [4, 7, 8, 10, 12, 15, 13, 17, 14, 5]
The Incremental Sublist Sum is : {1: 4, 2: 15, 3: 37, 4: 49}

Time complexity: O(n^2) because it uses a nested loop to iterate over all possible sublists.

Auxiliary space: O(n^2) because it creates a dictionary res that potentially stores all possible sublists of the input list test_list.

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32299 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6664 POSTS0 COMMENTS
Nicole Veronica
11837 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11895 POSTS0 COMMENTS
Shaida Kate Naidoo
6779 POSTS0 COMMENTS
Ted Musemwa
7054 POSTS0 COMMENTS
Thapelo Manthata
6738 POSTS0 COMMENTS
Umr Jansen
6744 POSTS0 COMMENTS