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].
# 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)) |
[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 code lst1 = [10, 20, 30, 40, 50] lst2 = [0, 2, 4] print(findElements(lst1, lst2)) |
[10, 30, 50]
Approach #3 : Using itemgetter()
# 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)) |
[10, 30, 50]
Approach #4 : Using numpy
# 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)) |
[10, 30, 50]
Recommended Posts:
- Python | Indices of sorted list of list elements
- Python | Group elements at same indices in a multi-list
- Python | Ways to find indices of value in list
- Python | Find indices with None values in given list
- Python | Get indices of True values in a binary list
- Python program to fetch the indices of true values in a Boolean list
- Python | Find sum of frequency of given elements in the list
- Python | Find missing elements in List
- Python program to find sum of elements in list
- Python | Find all elements count in list
- Python program to find N largest elements from a list
- Python | Find top K frequent elements from a list of tuples
- Python | Find common elements in list of lists
- Python | Find the list elements starting with specific letter
- Python | Sorting list of lists with similar list elements
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.



