Find Index of First Occurrence in List in Python (2 Examples)

 

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

Here is an overview:

Let’s dive into the Python code!

 

Create Example List of Integers

We will create the example Python list of integers that we will use in this tutorial. Therefore, in your preferred Python coding IDE, run the line of code below to create the example list of integers:

my_list = [1, 2, 3, 4, 3, 5]
 
print(my_list)
 
# [1, 2, 3, 4, 3, 5]
 
print(type(my_list))
 
# <class 'list'>

With the example list of integers created, we will now examine two ways to determine the index of the first occurrence of 3 in the list, since it occurs more than once.

We, therefore, create the variable element_to_find = 3, which will be called in both examples.
 

Example 1: Get Index of 3 Using for Loop & enumerate() Function

In this first example, we will use a for loop and the enumerate() function to get the index of the first occurrence of 3 in the list:

for index, element in enumerate(my_list):
    if element == element_to_find:
        print(index)
        break
 
# 2
 
print(type(index))
 
# <class 'int'>

The for loop iterates over the list of tuples that is returned by the enumerate() function in the form of (index, element).

Then, a conditional “if” statement is used to check when the iteration exactly equals the element to find. When it does, it will return its index, and the loop would break; thereby the index of the first occurrence of 3 in the list will be returned.

 

Example 2: Get Index of First Occurrence of 3 Using List Comprehension

In this next example, we will use list comprehension along with the enumerate() function to find the index of the first occurrence of 3 in the list:

index = [i for i, e in enumerate(my_list) if e == element_to_find][0]
 
print(index)
 
# 2
 
print(type(index))
 
# <class 'int'>

Here, we used list comprehension to iterate over the output of the enumerate() function and used a conditional “if” statement to determine when the iteration exactly equaled the element of interest and then returned the index of its first occurrence.

List comprehension works just like a for loop, only a more concise approach, with results always returned in a list.

With that, we have demonstrated 2 simple ways to return the index of the first occurrence of an element in a list in Python. I hope you found it helpful!
 

Video, Further Resources & Summary

Do you need more explanations on how to find the index of the first occurrence 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 the first occurrence 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 the first occurrence 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