Python | Accessing all elements at given list of indexes
Last Updated :
10 Aug, 2023
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:
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
test_list = [ 9 , 4 , 5 , 8 , 10 , 14 ]
index_list = [ 1 , 3 , 4 ]
print ( "Original list : " + str (test_list))
print ( "Original index list : " + str (index_list))
res_list = [test_list[i] for i in index_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: 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
test_list = [ 9 , 4 , 5 , 8 , 10 , 14 ]
index_list = [ 1 , 3 , 4 ]
print ( "Original list : " + str (test_list))
print ( "Original index list : " + str (index_list))
res_list = map (test_list.__getitem__, index_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: 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
test_list = [ 9 , 4 , 5 , 8 , 10 , 14 ]
index_list = [ 1 , 3 , 4 ]
print ( "Original list : " + str (test_list))
print ( "Original index list : " + str (index_list))
res_list = list (itemgetter( * index_list)(test_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 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 numpy as np
test_list = [ 9 , 4 , 5 , 8 , 10 , 14 ]
index_list = [ 1 , 3 , 4 ]
print ( "Original list : " + str (test_list))
print ( "Original index list : " + str (index_list))
res_list = np.take(test_list, index_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.
Please Login to comment...