Python | Find elements of a list by indices

• Last Updated : 18 Apr, 2019

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

 # 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 codelst1 = [10, 20, 30, 40, 50]lst2 = [0, 2, 4]print(findElements(lst1, lst2))
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.

 # 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 codelst1 = [10, 20, 30, 40, 50]lst2 = [0, 2, 4]print(findElements(lst1, lst2))
Output:
[10, 30, 50]

Approach #3 : Using itemgetter()

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

Approach #4 : Using numpy

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

My Personal Notes arrow_drop_up