The index function searches a list and returns the position of the first matched item in the specified list. For example:
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:
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:
#Python index function examples lst_num = [0, 5, 10, 15, 20, 25] int_pos = lst_num.index(10) print ("The position of item 10 = ", int_pos)
The output of the above code is:
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:
lst_str = ["X","B","A", "D","Z", "A" ,"C","A"] str_pos = lst_str.index("A") print ("Index of A = ", str_pos)
The output:
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.
lst_animals = ["cat", "dog", "cow", "bull"] animal_pos1 = lst_animals.index("cat") animal_pos2 = lst_animals.index("rat") print ("Position of cat = ", animal_pos1) print ("Position of rat = ", animal_pos2)
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:
#index with start argument lst_fruits = ["Apple","Mango","Strawberry","Banana","Apple","Currant"] fruit_pos = lst_fruits.index("Apple", 2) print ("Index of Apple from position 2 = ", fruit_pos)
The result of the above code is:
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 result 4, 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 the ending index.
If both arguments (start and end) are given then the index function searches the value within that limit.
If that searched item even exists in the given list but is beyond the given limit then ValueError occurs.
The example below shows using the start and end arguments:
#index with start and end arguments lst_animals = ["cat", "dog", "cow", "bull", "bat", "dog", "goat", "cow"] ani_pos = lst_animals.index("cow", 3, 6) print ("The position of cow = ", ani_pos)
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.