Find Index of Element in List in Python (3 Examples)

 

Hi! This tutorial will show you how to get the index of an item in a list in the Python programming language.

Here is an overview:

Let’s dive into the Python code!

 

Create Example Python List

Here, we will create the example Python list of string values that we will use in the examples in this tutorial. So, in your preferred Python IDE, run the code below to create the example Python list of string values:

my_list = ["Mary","John","Martha","Peter","Mark"]
 
print(my_list)
# ['Mary', 'John', 'Martha', 'Peter', 'Mark']
 
print(type(my_list))
# <class 'list'>

With the example Python list of string values created, we can now explore examples of how to determine the index of an item in the list. The element of interest is “Martha”. So, we can run the following code:

search_element = "Martha"

Let’s take a look at the first example!

 

Example 1: Get Index of Element Using index() Method

In this first example, we will use the index() method to return the index of the search element in the list:

index = my_list.index(search_element)
 
print(index)
# 2

The above example uses the index() method of the list to search for the element, which takes an argument called search_element and returns the index of the first occurrence of that element in the list, which is 2.

The index value is then stored in the variable index. Finally, the value of index is printed to the console using print().
 

Example 2: Get Index of Element Using for Loop & enumerate() Function

In this next example, we will use a for loop and Python’s enumerate() function to get the index of the search element in the list:

for index,element in enumerate(my_list):
  if element == search_element:
    print(index)
# 2

The example above uses a for loop, and the built-in enumerate() function to iterate through a list called my_list. For each element in the list, it checks if the element is equal to the variable search_element using the conditional if statement.

The enumerate() function generates a sequence of index-value pairs for each element in the list, and the for loop iterates through this sequence. If the element is equal to search_element, the code prints the current index value using the print() function.

If the search element is not present in the list, it will not print anything.

 

Example 3: Get Index of Element Using List Comprehension

In this last example, we will use list comprehension to return the index of the search element in the list:

index = [index for index in range(len(my_list)) if my_list[index] == search_element][0]
 
print(index)
# 2

Here, the list comprehension iterates over a range of integers from 0 to the length of my_list using the range() function. For each index value, it checks if the corresponding element in my_list is equal to search_element.

If the element is equal to search_element, the index value is added to the list index. The index [0] at the end of the list comprehension selects the first element of the resulting list.

This will print the index of the first occurrence of search_element in my_list, and if search_element is not contained in the list, an IndexError will be raised since the list comprehension will return an empty list.
 

So, with that, we have demonstrated, with 3 examples, how to find the index of an element in a list in Python. I hope you found this tutorial beneficial!

 

Video, Further Resources & Summary

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

In the video, we explain in some more detail how to find the index of an element in a list in Python.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown how to find the index of an element in a list in Python. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

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