Finding the Index of an Item in a Python List?

Better Stack Team
Updated on April 22, 2024

To find an index of the first occurrence of an element in a given list, you can use index method of List class with the element passed as an argument.

The syntax is the following:

 
my_list = [1,2,0,4,5,6,7,0,9]

my_list.index(0) # returns 2

You can also specify the start and end of the search area:

 
my_list.index(0, 3, 8) # returns 7

In the first example, we are looking for the first occurrence of 0 in the entire list from the start of the list to the end of the list. The first occurrence of 0 is at index 2, therefore 2 is returned.

In the second example, we specify that we want to start at index 3 and search up to index 8. Therefore the index 7 of the second 0 is returned because the first 0 is outside of the search area.