Python | Access to all elements in a given list of indices

Python Methods and Functions

Accessing an element from its index is easier in Python by simply using the [] operator in the list. But in certain situations, we are faced with challenges when we have multiple times of indices, and we need to get all the elements corresponding to these indices. Let's discuss some ways to achieve this.

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

Method # 1: Using the comprehension list
This task is easy to accomplish with a loop, and therefore pruning is the first method to start with this task. Iterate through the list of indices to get matching elements from the list into a new list — a rough method to accomplish this task.

# Python3 demo code
# get elements from indexes
# using comprehension list

 
# 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 for
# list items

res_list = [test_list [i] for i in index_list]

 
# result print

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] 

Method number 2 : Using map () + __getitem__
Another way to solve this particular problem — match one list to another and get index items and get matching matched items from the search list. This is a pretty quick way to accomplish this task.

# Python3 demo code
# get the 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
# list items

res_list = map (test_list .__ getitem__, index_list)

  
# print result

print ( "Resultant list:" + str (res_list))

Exit :

 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 the most pythonic and elegant method to accomplish this particular task ... This function archives the elements of the original list with the index required from the other, hence it is a fast way to accomplish this.

# Python3 demo code
# get items from indices
# using operator.itemgetter ()

from operator import itemgetter

 
# initializing lists

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

 
# using operator.itemgetter () for
# list items

res_list = list (itemgetter ( * index_list) (test_list))

 
# print 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] 




Get Solution for free from DataCamp guru