How to index a list with another list in Python

Indexing a list with another list results in a new list containing the elements of the original list at the indices specified in the indexing list. For example, indexing [“apple”, “pear”, “banana”, “peach”] with [1, 3] results in [“pear”, “peach”].

Solution for How to index a list with another list in Python : You can use a list comprehension to extract items from a list Use the syntax [list[index] for index in index_list] to get a list containing the elements in list at the indices in index_list.


how-to-index-a-list-with-another-list-in-python