Accessing an element from its index is an easier task in Python, just using the [] operator in a Python list does the trick. But in certain situations, we are presented with tasks when we have more than one index and we need to get all the elements corresponding to those indices. Let’s discuss certain ways to achieve this task.
Input: list = [9, 4, 5, 8, 10, 14] index_list = [1, 3, 4] Output: 4 8 10 Explanation:The output involve the value from the list which having the index from the index_list.
Accessing all elements at given list of indexes
Below are the methods that we will cover in this article:
- Using List Comprehension
- Using map() and __getitem__
- Using operator.itemgetter()
- Using numpy.take() function to access multiple elements in a list
Accessing all items on a given list using List Comprehension
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 the list into the new list is a brute method to perform this task.
Python3
# 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 to # elements from list res_list = [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 : [4, 8, 10]
Time Complexity: O(k), where k is the length of the index_list
.
Space Complexity:O(k)
Accessing multiple elements at given list using map() and __getitem__
Yet another method to achieve this particular task is to map one list with __getitem__ and get items of indexes and get corresponding matched elements from the search list. This is quite a quick way to perform this task.
Python3
# 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__ to # elements from list res_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 : [4, 8, 10]
Time Complexity: O(k), where k is the length of the index_list
.
Space Complexity:O(k)
Accessing multiple items in a given list using operator.itemgetter()
This operator.itemgetter() technique is the most pythonic and elegant method to perform this particular task. This function zips the elements of the original list with the index required from the other, hence the fasted method to achieve this task.
Python3
from operator import itemgetter # 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 operator.itemgetter() to # elements from list res_list = list (itemgetter( * index_list)(test_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 : [4, 8, 10]
Time complexity: The time complexity of this program is O(k), where k is the length of the index_list.
Space complexity: The space complexity of this program is O(k), since we are creating a new list with k elements using the itemgetter() function.
Access multiple elements in a list using numpy.take() function
Another efficient way to access multiple elements in a list at given indices is by using the numpy.take() function. Numpy is a popular library in Python used for numerical computing, and its take() function is specifically designed to perform this task.
Algorithm: Import the Numpy library then initialize the input list and index list. Use the numpy.take() function to retrieve elements from the input list at given indices.Print the original list, index list, and resultant list.
Python3
# import required libraries import numpy as np # initialize input list and index list test_list = [ 9 , 4 , 5 , 8 , 10 , 14 ] index_list = [ 1 , 3 , 4 ] # print original lists print ( "Original list : " + str (test_list)) print ( "Original index list : " + str (index_list)) # use numpy.take() to retrieve elements # from input list at given indices res_list = np.take(test_list, index_list) # print resultant list print ( "Resultant list : " + str (res_list)) |
Output:
Original list : [9, 4, 5, 8, 10, 14]
Original index list : [1, 3, 4]
Resultant list : [4 8 10]
Time Complexity: The time complexity of the numpy.take() function is O(k), where k is the length of the index_list. This function is optimized for performance and is generally faster than using a loop to iterate over the index list.
Space Complexity: The space complexity of this program is O(k), as we are creating a new numpy array with k elements to store the resultant list.