Open In App

Python | Selective indices Summation

Improve
Improve
Like Article
Like
Save
Share
Report

Accessing an element from its index is easier task in python, just using the [] operator in a list does the trick. But in certain situations we are presented with tasks when we have more than once indices and we need to get all the elements corresponding to those indices and then perform the summation. Lets discuss certain ways to achieve this task. 

Method #1 : Using List comprehension + sum() This task is easy to perform with a loop, and hence shorthand for it is the first method to start with this task. Iterating over the index list to get the corresponding elements from list into new list is brute method to perform this task. The task of summation is performed using sum(). 

Python3




# Python3 code to demonstrate
# Selective indices Summation
# using list comprehension + sum()
 
# initializing lists
test_list = [9, 4, 5, 8, 10, 14]
index_list = [1, 3, 4]
 
# printing original lists
print ("Original list : " + str(test_list))
print ("Original index list : " + str(index_list))
 
# using list comprehension + sum() to
# Selective indices Summation
res_list = sum([test_list[i] for i in index_list])
     
# printing result
print ("Resultant list : " + str(res_list))


Output : 

Original list : [9, 4, 5, 8, 10, 14]
Original index list : [1, 3, 4]
Resultant list : 22

Time Complexity: O(k), where k is the length of index_list.

Auxiliary Space: O(1). We are not using any additional data structure to store intermediate results, and the final result is stored in a single variable.

 Method #2 : Using map() + __getitem__ + sum() Yet another method to achieve this particular task is to map one list with other and get items of indexes and get corresponding matched elements from the search list. This is quite quick way to perform this task. The task of summation is performed using sum(). 

Python3




# Python3 code to demonstrate
# Selective indices Summation
# using map() + __getitem__ + sum()
 
# initializing lists
test_list = [9, 4, 5, 8, 10, 14]
index_list = [1, 3, 4]
 
# printing original lists
print ("Original list : " + str(test_list))
print ("Original index list : " + str(index_list))
 
# using map() + __getitem__ + sum() to
# Selective indices Summation
res_list = sum(list(map(test_list.__getitem__, index_list)))
     
# printing result
print ("Resultant list : " + str(res_list))


Output : 

Original list : [9, 4, 5, 8, 10, 14]
Original index list : [1, 3, 4]
Resultant list : 22

The time complexity of the given code is O(n), where n is the length of the index list, as the map() and sum() functions are applied only to the selected indices.

The auxiliary space complexity of the code is O(n), as a new list is created using the map() function to retrieve elements at the selected indices, and then the sum() function is applied on this list to obtain the final result.

Method #3: Using a loop

Using a loop to iterate through the index list and adding the corresponding elements from the test_list.

Python3




# Python3 code to demonstrate
# Selective indices Summation
# using a loop
 
# initializing lists
test_list = [9, 4, 5, 8, 10, 14]
index_list = [1, 3, 4]
 
# printing original lists
print("Original list : " + str(test_list))
print("Original index list : " + str(index_list))
 
# using a loop to Selective indices Summation
res_list = 0
for i in index_list:
    res_list += test_list[i]
     
# printing result
print("Resultant list : " + str(res_list))


Output

Original list : [9, 4, 5, 8, 10, 14]
Original index list : [1, 3, 4]
Resultant list : 22

The time complexity of this code is O(n), where n is the number of elements in the index_list.
The auxiliary space complexity of this code is O(1), since the only additional space used is for the res_list variable, which is a single integer value.

Method 4:  Using the reduce() function from the functools 

Using a list comprehension to extract the elements at the specified indices, and then uses reduce() with a lambda function to sum up the elements in the new list.

Python3




from functools import reduce
 
test_list = [9, 4, 5, 8, 10, 14]
index_list = [1, 3, 4]
 
res_list = reduce(lambda x, y: x+y, [test_list[i] for i in index_list])
 
print("Resultant list : " + str(res_list))


Output

Resultant list : 22

Time complexity: O(n), where n is the number of elements in the index_list. 
Auxiliary space: O(m), where m is the number of elements in the index_list. 

Method 5: Using NumPy

This method uses NumPy to extract the elements from the test_list based on their indices present in the index_list. The np.take() function is used to extract the required elements and then the np.sum() function is used to calculate the sum of the extracted elements.

Here’s the step-by-step approach:

Import NumPy module.

Initialize the test_list and index_list.

Use np.take() function to extract the elements from test_list based on their indices present in index_list.

Use np.sum() function to calculate the sum of the extracted elements.

Print the resultant list.

Python3




# Python3 code to demonstrate
# Selective indices Summation
# using NumPy
 
# import NumPy module
import numpy as np
 
# initializing lists
test_list = [9, 4, 5, 8, 10, 14]
index_list = [1, 3, 4]
 
# using NumPy to Selective indices Summation
res_list = np.sum(np.take(test_list, index_list))
 
# printing result
print("Resultant list : " + str(res_list))


OUTPUT:

Resultant list : 22

Time Complexity: O(n) (linear time complexity as we are iterating through the index_list)

Auxiliary Space: O(1) (constant space complexity as we are not using any additional data structures)

Method #6: Using list slicing

Step-by-step approach:

Initialize test_list and index_list.
Use list slicing to get the elements from test_list whose indices are in index_list.
Use the sum function to add up the elements obtained in step 2.
Print the result.

Python3




# initializing lists
test_list = [9, 4, 5, 8, 10, 14]
index_list = [1, 3, 4]
 
# using list slicing to Selective indices Summation
res_list = sum(test_list[i] for i in index_list)
 
# printing result
print("Resultant list : " + str(res_list))


Output

Resultant list : 22

Time complexity: O(k), where k is the number of elements in index_list.
Auxiliary space: O(1), as only a constant amount of memory is used for res_list.



Last Updated : 05 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads