 # Python | Find elements of a list by indices

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 ie 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 accomplish the above task.

Approach # 1: Naive (List of Understandings)

The first approach to finding the desired elements — use a list comprehension. We go through & # 39; lst2 & # 39; and for each i- th element we display lst1 [i].

 ` # Python3 program for finding elements ` ` # list by indexes provided 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)) `

Exit:

` [10, 30 , 50] `

Approach # 2 Using ` map () ` Python `map()`

We can also use Python`s ` map () ` method where we apply ` lst1 .__ getitem__ ` to lst2, which returns ` lst1 [i] ` for each element & # 39; i & # 39; from lst2.

 ` # Python3 program for finding items ` ` # list by indexes provided 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)) `

Exit :

` [10, 30, 50] `

Approach # 3 Using ` itemgetter ( ) `

 ` # Python3 program for finding elements ` ` # list by index, provided 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)) ```

Output :

` [10, 30, 50] `

Approach # 4 Use ` numpy `

 ` # Python3 program to find elements ` ` # list by indices 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)) `

Output :

` [10, 30, 50] `