Change language

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

## Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

NUMPYNUMPY

glob exclude pattern

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

NUMPYNUMPY

Python CSV error: line contains NULL byte

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically