Python | Find elements of a list by indices

Given two lists with elements and indices, write a Python program to find elements of list 1 at indices present in list 2.

Examples:

Input : lst1 = [10, 20, 30, 40, 50]
        lst2 = [0, 2, 4]
Output : [10, 30, 50]
Explanation: 
Output elements at indices 0, 2 and 4 i.e 10, 30 and 50 respectively. 

Input : lst1 = ['Hello', 'geeks', 'for', 'geeks']
        lst2 = [1, 2, 3]
Output : ['geeks', 'for', 'geeks']

 
Below are some Pythonic approaches to do the above task.



Approach #1 : Naive(List comprehension)

The first approach to find the desired elements is to use list comprehension. We traverse through ‘lst2’ and for each ith element, we output lst1[i].

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to Find elements of a 
# list by indices present in another list
  
def findElements(lst1, lst2):
    return [lst1[i] for i in lst2]
              
# Driver code
lst1 = [10, 20, 30, 40, 50]
lst2 = [0, 2, 4]
print(findElements(lst1, lst2))

chevron_right


Output:

[10, 30, 50]

 
Approach #2 : Using Python map()



We can also use Python map() method where we apply lst1.__getitem__ function on lst2 which return lst1[i] for each element ‘i’ of lst2.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to Find elements of a 
# list by indices present in another list
  
def findElements(lst1, lst2):
    return list(map(lst1.__getitem__, lst2))
              
# Driver code
lst1 = [10, 20, 30, 40, 50]
lst2 = [0, 2, 4]
print(findElements(lst1, lst2))

chevron_right


Output:

[10, 30, 50]

 
Approach #3 : Using itemgetter()

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to Find elements of a 
# list by indices present in another list
from operator import itemgetter 
  
def findElements(lst1, lst2):
    return list((itemgetter(*lst2)(lst1)))
              
# Driver code
lst1 = [10, 20, 30, 40, 50]
lst2 = [0, 2, 4]
print(findElements(lst1, lst2))

chevron_right


Output:

[10, 30, 50]

 
Approach #4 : Using numpy

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to Find elements of a 
# list by indices present in another list
import numpy as np 
  
def findElements(lst1, lst2):
    return list(np.array(lst1)[lst2])
              
# Driver code
lst1 = [10, 20, 30, 40, 50]
lst2 = [0, 2, 4]
print(findElements(lst1, lst2))

chevron_right


Output:

[10, 30, 50]


My Personal Notes arrow_drop_up


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.