Get Index of Multiple List Elements in Python (3 Examples)

 

In this article, you’ll learn how to get the index of multiple elements in a Python list.

The content of the post looks as follows:

Let’s go straight to the code!

 

Example List

At the start, we will need to create a sample list to use in the following examples.

my_list = [10, "apple", 20, "banana", 30, "orange"]
print(my_list)
# [10, 'apple', 20, 'banana', 30, 'orange']

As shown, we have created a Python list named my_list, which contains three integers (e.g., 10) and three strings (e.g., “banana”).

Let’s say we want to find the index of each of the strings “apple”, “banana” and “orange”. To do so, we will first create the elements list as follows.

elements = ["apple", "banana", "orange"]

Now, let’s get the index of the elements list!

 

Example 1: Use List Comprehension to Get Multiple Indexes in List

In this example, we will use list comprehension to get the indexes from our selected elements in my_list.

indexes = [i for i in range(len(my_list)) if my_list[i] in elements]
print(indexes)
# [1, 3, 5]

As you can see, the list comprehension has created a list of all the indexes where the corresponding element in my_list is in the elements list. In this case, the resulting list is [1, 3, 5], which are the indexes of each of the elements list.

 

Example 2: Use for Loop to Get Multiple Indexes in List

In this next example, we will use a for loop to get the index of each element in elements in my_list.

indexes = []
for element in elements:
    index = my_list.index(element)
    indexes.append(index)
print(indexes)
# [1, 3, 5]

Here, we have first created an empty list called indexes that we will fill with the indexes of elements. The for loop iterates over each element in the elements list and, for each of these elements, the index() method is applied on my_list to find the corresponding indexes in my_list.

Note that if there are multiple occurrences, we will only get the index of the first element using this method.

 

Example 3: Use numpy.where() Method to Get Multiple Indexes in List

This last example shows how to use the NumPy library to get the indexes of multiple elements in a list. To do so, we will first install and import NumPy.

pip install numpy
import numpy as np

Now, we can proceed with the example.

my_array = np.array(my_list)
indexes = np.where(np.isin(my_array, elements))[0].tolist()
print(indexes)
# [1, 3, 5]

This code first converts my_list into a NumPy array using np.array(). Then, the np.isin() method is used to create a boolean array that is ‘True’ for elements that are in the elements list, and ‘False’ otherwise.

The np.where() function returns a tuple, where the first element is the array of indices that satisfy the condition. Since we are primarily interested in this array of indices. To access this array from the tuple, we need to use [0], which is an index operation that accesses the first element in the tuple.

 

Video, Further Resources & Summary

Do you need more explanations on how to get the index of multiple elements in a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

 

The YouTube video will be added soon.

 

Moreover, you could have a look at some related tutorials on Statistics Globe:

This post has shown how to get the indexes of multiple elements in a list in Python. In case you have further questions, don’t hesitate to let me know in the comments section.

 

Paula Villasante Soriano Statistician & R Programmer

This page was created in collaboration with Paula Villasante Soriano. Please have a look at Paula’s author page to get further information about her academic background and the other articles she has written for Statistics Globe.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top