Python Tuple index()


Python Tuple index() method

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

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

In this tutorial you will learn the syntax and usage of Tuple index() method with examples.

Syntax of Tuple index()

The syntax of Tuple index() method is

tuple.index(value, start, stop)

Parameters

Tuple 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 tuple.
startOptional
The index, where searching for the value starts.
stopOptional
The index, where searching for the value stops.
Tuple index() parameters

Return Value

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

Exceptions

Tuple index() method can raise ValueError exception.

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

Examples

1. Get the index of value 'cherry' in the tuple in Python

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

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

Python Program

my_tuple = ('apple', 'banana', 'cherry', 'fig')
value_index = my_tuple.index('cherry')
print(f"Index : {value_index}")

Output

Index : 2

Explanation

('apple', 'banana', 'cherry', 'fig')    ← given tuple
 0        1         2         3         ← indices of items in tuple
                    'cherry'            ← value to search for
                    2    ← is the index of the search value

2. Get the index of value 'cherry' in the tuple with search from a specific start index

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

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

Python Program

my_tuple = ('apple', 'banana', 'cherry', 'fig', 'cherry')
value_index = my_tuple.index('cherry', 3)
print(f"Index : {value_index}")

Output

Index : 4

Explanation

('apple', 'banana', 'cherry', 'fig', 'cherry')    ← given tuple
 0        1         2         3      4            ← indices of items in tuple
                              ↑
                     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 tuple with search in [start, stop) indices of tuple

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

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

Python Program

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

Output

Index : 2

Explanation

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

4. Tuple index() method when the value is not present in tuple

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

Python Program

my_tuple = ('apple', 'banana', 'cherry', 'fig')
value_index = my_tuple.index('mango')
print(f"Index : {value_index}")

Output

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

Summary

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