Python | Selective indices Summation
Last Updated :
05 May, 2023
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))
OutputOriginal 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))
OutputResultant 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))
OutputResultant 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.
Similar Reads
Python | Selective Keys Summation Sometimes while working with Python dictionaries, we might have a problem in which we require to just sum the selective key values from the dictionary. This problem can occur in the web development domain. Letâs discuss certain ways in which this problem can be solved. Method #1 : Using list compreh
6 min read
Python - Selective Key Values Summation Sometimes, while working with Python dictionaries, we can have a problem in which we desire to get summation of certain keys' values in dictionary. This kind of application can have usecase in many domains such as day-day programming. Let's discuss certain ways in which this task can be performed. I
8 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 | Selective Records Value Summation Sometimes, while using a list of tuples, we come across a problem in which we have e certain list of keys and we just need the summation of values of those keys from the list of tuples. This has a utility in rating or summation of specific entities. Letâs discuss certain ways in which this can be do
11 min read
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
7 min read
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