Python | Accessing all elements at given list of indexes

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. Lets discuss certain ways to achieve this task.

Input : list = [9, 4, 5, 8, 10, 14]
        index_list = [1, 3, 4]
Output : 4 8 10

Method #1 : 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 list into new list is brute method to perform this task.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# to get elements from indices
# using list comprehension
  
# 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))

chevron_right


Output :

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

Method #2 : Using map() + __getitem__
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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# to get elements from indices
# using map() + __getitem__
  
# 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))

chevron_right


Output :

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

Method #3 : Using operator.itemgetter()
This technique is 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# to get elements from indices
# using operator.itemgetter()
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))

chevron_right


Output :

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.