Hit enter after type your search item

Python List Index Function

The index function searches a list and returns the position of the first matched item in the specified list. For example:

Lst_index = list_name.index(“a”)

A few points to be noted about the Python List Index function:

  • The index starts at zero.
  • If there are more than one occurrences found for the searched value then index function returns the position of first matched element.
  • If no element is found then a ValueError is raised.

Syntax of list index function

The general syntax for using the list index function is:

list.index(item_to_search)

A simple example of the list index function

In this example, we have a list of five numeric elements. Then we used index function and searched for value “10” in the list. See what we get:


The output of the above code is:

The position of item 10 =  2

The example of multiple occurrences

In the list below, we have elements with multiple occurrences. The item “A” exists three times in the list and this is what we searched in the index function as well. See what index function returns:


The output:

Index of A =  2

You can see, the item “A” exists three times in the list, however, the index function returned the lowest index i.e. 2.

What happens if a value is not found in the list?

Now, let us search a term in the list that does not exist.


The result:

    animal_pos2 = lst_animals.index(“rat”)

ValueError: ‘rat’ is not in list

Specifying the start index example

You may also specify the starting position in the Python list for searching the index of an item. This is particularly useful for searching item with duplicate occurrences.

In the following example, we have a few duplicate items in the list of strings. Then, index function is used to return the position of an item with the starting index argument:


The result of the above code is:

Index of Apple from position 2 =  4

Although, the first occurrence of the item “Apple” is 0, however, as we provided start index 2 so the index function started searching the list from the second item. Therefore, we got the result 4 which is the second occurrence of the “Apple”.

The example of start and end indices

Not only you may provide the starting position but to limit the searching a list, you may also specify ending index. If both arguments (start and end) are given then index function searches the value within that limit.

If that searched item even exists in the given list but it is beyond the given limit then ValueError occurs.

The example below shows using the start and end arguments:


The result is an error:

    ani_pos = lst_animals.index(“cow”, 3, 6)

ValueError: ‘cow’ is not in list

Even though the “cow” exists twice in the list but we provided 3 and 6 positions to search in the list. The first occurrence is 2 while the second is 7 which is not within the limit we specified in the index function, so the ValueError generated.

This div height required for enabling the sticky sidebar