Python List index()

Python List index() method

Python List index() method is used to find the index (position) of the specified value in the list.

The search for the specified value happens from starting off the list to the ending of the list. This is the direction of search.

Syntax of List index()

The syntax of List index() method is

list.index(value, start, stop)

Parameters

index() method can take three parameters. Let us see those parameters, and their description.

ParameterDescription
valueMandatory
A value whose index to be found in the list.
startOptional
The index, where searching for the value starts.
stopOptional
The index, where searching for the value stops.

Return Value

index() method returns an integer value representing the index of the specified value in the list.

Exceptions

index() method can raise ValueError exception.

If the specified value is not present in the list, then index() method raises ValueError.

Examples

1. Get the index of value ‘cherry’ in the list in Python

In the following program, we define a list my_list with four string values. We have to find the index of the value 'cherry' in this list.

Call index() method on the list, and pass the value as argument.

Python Program

my_list = ['apple', 'banana', 'cherry', 'fig']
value_index = my_list.index('cherry')
print(f"Index : {value_index}")
Run Code Copy

Output

Index : 2

Explanation

['apple', 'banana', 'cherry', 'fig']    ← given list
 0        1         2         3         ← indices of items in list
                    'cherry'            ← value to search for
                    2    ← is the index of the search value

2. Get the index of value ‘cherry’ in the list with search from a specific start index

In the following program, we define a list my_list with five string values. We have to find the index of the value 'cherry' in this list with search happening from a start index of 3.

Call index() method on the list, and pass the search value 'cherry' and start index 3 as arguments.

Python Program

my_list = ['apple', 'banana', 'cherry', 'fig', 'cherry']
value_index = my_list.index('cherry', 3)
print(f"Index : {value_index}")
Run Code Copy

Output

Index : 4

Explanation

['apple', 'banana', 'cherry', 'fig', 'cherry']    ← given list
 0        1         2         3      4            ← indices of items in list
                              ↑
                     search starts from this index
                                     'cherry'            ← value to search for
                                     4    ← is the index of the search value

3. Get the index of value ‘cherry’ in the list with search in [start, stop) indices of list

In the following program, we define a list my_list with five string values. We have to find the index of the value 'cherry' in this list with search happening from a start index of 1, and stoping at an index of 3.

Call index() method on the list, and pass the search value 'cherry', start index 1, and stop index 3 as arguments.

Python Program

my_list = ['apple', 'banana', 'cherry', 'fig', 'cherry']
value_index = my_list.index('cherry', 0, 3)
print(f"Index : {value_index}")
Run Code Copy

Output

Index : 2

Explanation

['apple', 'banana', 'cherry', 'fig', 'cherry']    ← given list
 0        1         2         3      4            ← indices of items in list
↑                             ↑ 
start                         stop
                 'cherry'   ← value to search for
                    2       ← is the index of the search value

4. List index() method when the value is not present in list

If the search value is not present in the list, then the index() method throws ValueError exception, as shown in the following program.

Python Program

my_list = ['apple', 'banana', 'cherry', 'fig']
value_index = my_list.index('mango')
print(f"Index : {value_index}")
Run Code Copy

Output

Traceback (most recent call last):
  File "/Users/pythonexamplesorg/main.py", line 2, in <module>
    value_index = my_list.index('mango')
                  ^^^^^^^^^^^^^^^^^^^^^^
ValueError: 'mango' is not in list

Summary

In this tutorial of Python Examples, we learned about List index() method, how to use index() method to find the index of a value in the list, with syntax and examples.

Related Tutorials

Code copied to clipboard successfully 👍