Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Accessing all elements at given list of indexes

Improve Article
Save Article
Like Article
  • Last Updated : 14 Mar, 2023
Improve Article
Save Article
Like Article

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 indices 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

Using List comprehension Accessing all items a given list

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]

Using map() + __getitem__  Accessing multiple elements at given list 

Yet another method to achieve this particular task is to map one list with another 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]

Using operator.itemgetter() Accessing multiple items in a given list

This 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]

The time complexity of this program is O(k), where k is the length of the index_list.

The space complexity of this program is O(k), since we are creating a new list with k elements using the itemgetter() function.


My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!