Python | Custom Index Range Summation

Development and sometimes machine learning applications require splitting lists into smaller list in a custom way, i.e on certain values on which split has to be performed and then summation. This is quite a useful utility to have knowledge about. Lets discuss certain ways in which this task can be performed.

Method #1 : Using list comprehension + zip() + sum()
By coupling the power of list comprehension and zip(), this task can be achieved. In this we zip beginning and end of list and then keep slicing the list as they arrive and cutting off new lists from them. The task of finding summation is performed using sum().

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# Custom Index Range Summation
# using list comprehension + zip() + sum()
  
# initializing string 
test_list = [1, 4, 5, 6, 7, 3, 5, 9, 2, 4]
  
# initializing split index list 
split_list = [2, 5, 7]
  
# printing original list
print ("The original list is : " + str(test_list))
  
# printing original split index list
print ("The original split index list : " + str(split_list))
  
# using list comprehension + zip() + sum()
# Custom Index Range Summation
res = [sum(test_list[i : j]) for i, j in zip([0] + 
                 split_list, split_list + [None])]
  
# printing result
print ("The splitted lists summation are : " + str(res))

chevron_right


Output :



The original list is : [1, 4, 5, 6, 7, 3, 5, 9, 2, 4]
The original split index list : [2, 5, 7]
The splitted lists summation are : [5, 18, 8, 15]

 

Method #2 : Using itertools.chain() + zip() + sum()
The task performed by the list comprehension function of getting the split chunks can also be done using chain function. This is more useful when we wish to handle larger lists as this method is more efficient. The task of finding summation is performed using sum().

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# Custom Index Range Summation
# using itertools.chain() + zip()
from itertools import chain
  
# initializing string 
test_list = [1, 4, 5, 6, 7, 3, 5, 9, 2, 4]
  
# initializing split index list 
split_list = [2, 5, 7]
  
# printing original list
print ("The original list is : " + str(test_list))
  
# printing original split index list
print ("The original split index list : " + str(split_list))
  
# using itertools.chain() + zip()
# Custom Index Range Summation
temp = zip(chain([0], split_list), chain(split_list, [None]))
res = list(sum(test_list[i : j]) for i, j in temp)
  
# printing result
print ("The splitted lists summations are : " + str(res))

chevron_right


Output :

The original list is : [1, 4, 5, 6, 7, 3, 5, 9, 2, 4]
The original split index list : [2, 5, 7]
The splitted lists summation are : [5, 18, 8, 15]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.