Open In App

Python | Custom List slicing Sum

The problem of slicing a list has been dealt earlier, but sometimes we need to perform the slicing in variable lengths and its summation according to the input given in other list. This problem has its potential application in web development. Let’s discuss certain ways in which this can be done. 

Method #1 : Using itertools.islice() + sum() + list comprehension The list comprehension can be used to iterate through the list and the component issue is solved using the islice function. Summation is performed by sum(). 






# Python3 code to demonstrate
# Custom List slicing Sum
# using itertools.islice() + sum() + list comprehension
from itertools import islice
 
# initializing test list
test_list = [1, 5, 3, 7, 8, 10, 11, 16, 9, 12]
 
# initializing slice list
slice_list = [2, 1, 3, 4]
 
# printing original list
print("The original list : " + str(test_list))
 
# printing slice list
print("The slice list : " + str(slice_list))
 
# using itertools.islice() + sum() + list comprehension
# Custom List slicing Sum
temp = iter(test_list)
res = [sum(list(islice(temp, part))) for part in slice_list]
 
# print result
print("The variable sliced sum list is : " + str(res))

Output : 
The original list : [1, 5, 3, 7, 8, 10, 11, 16, 9, 12]
The slice list : [2, 1, 3, 4]
The variable sliced sum list is : [6, 3, 25, 48]

Time complexity: O(n*n) where n is the number of elements in the list “test_list”.



Auxiliary space: O(n) where n is the number of elements in the list “test_list”.

  Method #2 : Using zip() + accumulate() + sum() + list slicing Apart from using the list comprehension to perform the task of binding, this method uses zip function to hold sublist element together, accumulate function joins the elements, and slicing is used to construct the required slicing. Summation is performed by sum(). 




# Python3 code to demonstrate
# Custom List slicing Sum
# using zip() + accumulate() + sum() + list slicing
from itertools import accumulate
 
# initializing test list
test_list = [1, 5, 3, 7, 8, 10, 11, 16, 9, 12]
 
# initializing slice list
slice_list = [2, 1, 3, 4]
 
# printing original list
print("The original list : " + str(test_list))
 
# printing slice list
print("The slice list : " + str(slice_list))
 
# using zip() + accumulate() + sum() + list slicing
# Custom List slicing Sum
res = [sum(test_list[i - j: i]) for i, j in zip(accumulate(slice_list), slice_list)]
 
# print result
print("The variable sliced sum list is : " + str(res))

Output : 
The original list : [1, 5, 3, 7, 8, 10, 11, 16, 9, 12]
The slice list : [2, 1, 3, 4]
The variable sliced sum list is : [6, 3, 25, 48]

Time Complexity: O(n*n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.

METHOD 3: Using a loop and keeping track of the current slice’s starting and ending indices.

Step-by-step approach:

Below is the implementation of the above approach:




# Python3 code to demonstrate
# Custom List slicing Sum
# using a loop
 
# initializing test list
test_list = [1, 5, 3, 7, 8, 10, 11, 16, 9, 12]
 
# initializing slice list
slice_list = [2, 1, 3, 4]
 
# printing original list
print("The original list : " + str(test_list))
 
# printing slice list
print("The slice list : " + str(slice_list))
 
# using a loop
# Custom List slicing Sum
res = []
start_index = 0
for slice_len in slice_list:
    end_index = start_index + slice_len
    slice_sum = sum(test_list[start_index:end_index])
    res.append(slice_sum)
    start_index = end_index
 
# print result
print("The variable sliced sum list is : " + str(res))

Output
The original list : [1, 5, 3, 7, 8, 10, 11, 16, 9, 12]
The slice list : [2, 1, 3, 4]
The variable sliced sum list is : [6, 3, 25, 48]

Time complexity: O(n*m), where n is the length of the test list and m is the length of the slice list. 
Auxiliary space: O(m) to store the result list.

Method #4: Using numpy.cumsum() and numpy.take() to get the sliced sum.

  1. Import numpy module
  2. Create a numpy array from the test_list
  3. Calculate the cumulative sum of the numpy array using numpy.cumsum()
  4. Initialize an empty list res for storing the sliced sum.
  5. Iterate through the slice_list and for each element in the slice_list:
    a. Calculate the start and end index using the previous end index and the current slice length
    b. Use numpy.take() to get the slice of the cumulative sum between start and end indices
    c. Calculate the sliced sum by taking the difference of the elements in the slice
    d. Append the sliced sum to the res list
  6. Print the res list




import numpy as np
 
# initializing test list
test_list = [1, 5, 3, 7, 8, 10, 11, 16, 9, 12]
 
# initializing slice list
slice_list = [2, 1, 3, 4]
 
# convert test_list to numpy array
arr = np.array(test_list)
 
# calculate cumulative sum of the array
cumulative_sum = np.cumsum(arr)
 
# initialize res list
res = []
 
# iterate through slice_list
start_index = 0
for slice_len in slice_list:
    end_index = start_index + slice_len
    # get slice of cumulative sum between start and end indices
    slice_cumulative_sum = np.take(cumulative_sum, range(start_index, end_index))
    # calculate sliced sum
    slice_sum = slice_cumulative_sum[-1] - slice_cumulative_sum[0] + arr[start_index]
    # append to res list
    res.append(slice_sum)
    # update start_index
    start_index = end_index
 
# print result
print("The variable sliced sum list is : " + str(res))

OUTPUT:

The variable sliced sum list is : [6, 3, 25, 48]

Time complexity: O(n + m), where n is the length of the test_list and m is the length of the slice_list
Auxiliary space: O(n), where n is the length of the test_list

Method #5: Using heapq:
Algorithm:

  1. Initialize the test_list and slice_list variables with the given values.
  2. Print the original and slice list.
  3. Initialize an empty list res to store the sliced sums.
  4. Initialize the start_index variable to 0.
  5. Iterate over the elements of the slice_list using a loop:
  6. Calculate the end_index by adding the current slice length to the start_index.
  7. Slice the test_list from the start_index to the end_index.
  8. Calculate the sum of the sliced elements using the sum() function.
  9. Append the sum to the res list.
  10. Update the start_index variable to the end_index for the next iteration.
  11. Print the res list.




import heapq
 
# initializing test list
test_list = [1, 5, 3, 7, 8, 10, 11, 16, 9, 12]
 
# initializing slice list
slice_list = [2, 1, 3, 4]
 
# printing original list
print("The original list : " + str(test_list))
 
# printing slice list
print("The slice list : " + str(slice_list))
 
# using a loop
# Custom List slicing Sum
res = []
start_index = 0
for slice_len in slice_list:
    end_index = start_index + slice_len
    slice_sum = sum(test_list[start_index:end_index])
    res.append(slice_sum)
    start_index = end_index
 
# print result
print("The variable sliced sum list is : " + str(res))
 
 
#This code is contributed by Jyothi pinjala

Output
The original list : [1, 5, 3, 7, 8, 10, 11, 16, 9, 12]
The slice list : [2, 1, 3, 4]
The variable sliced sum list is : [6, 3, 25, 48]

Time complexity:
The time complexity of this algorithm is O(n * m), where n is the length of the test_list and m is the length of the slice_list. This is because we need to iterate over the slice_list and slice the test_list accordingly. The sum() function also takes O(m) time to calculate the sum of the sliced elements.

Space complexity:
The space complexity of this algorithm is O(m), where m is the length of the slice_list. This is because we need to store the sliced sums in the res list, which has a maximum length of m. The space complexity of the test_list and slice_list is not included as they are given inputs.


Article Tags :