Finding the index of an item in a Python list?

Better Stack Team
Updated on January 26, 2023

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.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

We are hiring.

Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.

Explore all positions →