Python | Interval List Summation Last Updated : 07 May, 2023 Comments Improve Suggest changes Like Article Like Report There are numerous ways to initialize the list with the elements, but sometimes, its required to initialize the lists with the numbers in a sliced way and find its summation. This can be custom and hence knowledge of this can come handy. Let’s discuss certain ways in which this can be done. Method #1 : Using list comprehension + enumerate() + sum() The list comprehension can do the possible iteration part and enumerate can help in the part of logic and checking for the valid elements required in the list. The sum() is used to perform summation. Python3 # Python3 code to demonstrate # Interval List Summation # using list comprehension + enumerate() + sum() # initializing lists test_list = list(range(50)) # printing original list print ("The original list is : " + str(test_list)) # interval elements N = 5 # interval difference K = 15 # using list comprehension + enumerate() + sum() # Interval List Summation res = sum([i for j, i in enumerate(test_list) if j % K < N ]) # printing result print ("The modified range sum list : " + str(res)) Output : The original list is : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49] The modified range sum list : 490 Time Complexity: O(n) where n is the number of elements in the list “test_list”. The list comprehension + enumerate() + sum() is used to perform the task and it takes O(n) time.Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list “test_list”. Method #2 : Using itertools.compress() + itertools.cycle() + sum() The above two function can combine to facilitate the solution of the discussed problem. The cycle function can to the task of repetition and the compress function can be beneficial when it comes to clubbing the segments together. The sum() is used to perform summation. Python3 # Python3 code to demonstrate # Interval List Summation # using itertools.compress() + itertools.cycle() + sum() from itertools import compress, cycle # initializing lists test_list = list(range(50)) # printing original list print ("The original list is : " + str(test_list)) # interval elements N = 5 # interval difference K = 15 # using itertools.compress() + itertools.cycle() + sum() # Interval List Summation func = cycle([True] * N + [False] * (K - N)) res = sum(list(compress(test_list, func))) # printing result print ("The modified range sum list : " + str(res)) Output : The original list is : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49] The modified range sum list : 490 Time Complexity: O(n*n), where n is the number of elements in the list “test_list”.Auxiliary Space: O(1), constant extra space is required Comment More infoAdvertise with us Next Article Python | Summation of tuples in list manjeet_04 Follow Improve Article Tags : Python Python Programs Python list-programs Practice Tags : python Similar Reads Python - Index Value Summation List To access the elements of lists, there are various methods. But sometimes we may require to access the element along with the index on which it is found and compute its summation, and for that, we may need to employ different strategies. This article discusses some of those strategies. Method 1: Nai 4 min read Python | Summation of tuples in list Sometimes, while working with records, we can have a problem in which we need to find the cumulative sum of all the values that are present in tuples. This can have applications in cases in which we deal with a lot of record data. Let's discuss certain ways in which this problem can be solved. Metho 7 min read Python | Alternate element summation in list The problem of getting summation of a list is quite generic and we might some day face the issue of getting the summation of alternate elements and get the list of 2 elements containing summation of alternate elements. Let's discuss certain ways in which this can be performed. Method #1 : Using list 5 min read Python - K Summation from Two lists Sometimes, while working with Python lists we can have a problem in which we need to find summation of lists taking elements from two different lists. This kind of application can come in different domains such as web development. Lets discuss certain ways in which this task can be performed. Method 6 min read Python | Triple List Summation There can be an application requirement to append elements of 2-3 lists to one list and perform summation. This kind of application has a potential to come into the domain of Machine Learning or sometimes in web development as well. Letâs discuss certain ways in which this particular task can be per 3 min read Element indices Summation - Python Our task is to calculate the sum of elements at specific indices in a list. This means selecting elements at specific positions and computing their sum. Given a list and a set of indices, the goal is to compute the sum of elements present at those indices. For example, given the list [10, 20, 30, 40 3 min read Python | Pair summation of list elements Sometimes, while working with Python list, one can have a problem in which one needs to find perform the summation of list in pair form. This is useful as a subproblem solution of bigger problem in web development and day-day programming. Let's discuss certain ways in which this problem can be solve 4 min read Summation Matrix columns - Python The task of summing the columns of a matrix in Python involves calculating the sum of each column in a 2D list or array. For example, given the matrix a = [[3, 7, 6], [1, 3, 5], [9, 3, 2]], the goal is to compute the sum of each column, resulting in [13, 13, 13]. Using numpy.sum()numpy.sum() is a hi 2 min read Python - Dual Tuple Alternate summation Given dual tuple, perform summation of alternate elements, i.e of indices alternatively. Input : test_list = [(4, 1), (5, 6), (3, 5), (7, 5)] Output : 18 Explanation : 4 + 6 + 3 + 5 = 18, Alternatively are added to sum. Input : test_list = [(4, 1), (5, 6), (3, 5)] Output : 13 Explanation : 4 + 6 + 3 8 min read Python - Summation of float string list Sometimes, while working with Python list, we can have a problem in which we need to find summation in list. But sometimes, we donât have a natural number but a floating-point number in string format. This problem can occur while working with data, both in web development and Data Science domain. Le 7 min read Like