Python Tutorial Python Advanced Python Resources Python Libraries
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

Python - Tuple index() Method



The Python tuple index() method returns the index number of first occurrence of the specified element in the tuple. It returns the zero-based index.

The method raises a ValueError, if the specified element is not found.

Syntax

tuple.index(elem, start, end)

Parameters

elem Required. Specify the value of the element which need to be searched in the tuple.
start Optional. Specify the starting point of the search in the tuple.
end Optional. Specify the end point of the search in the tuple.

Return Value

Returns the index number of first occurrence of an specified element in the tuple.

Example:

In the example below, index() method is used to find out index number of first occurrence of an element called 'MAR' in the tuple called MyTuple

MyTuple = ('JAN', 'FEB', 'MAR', 'APR', 'MAR')

x = MyTuple.index('MAR') 
print(x)

The output of the above code will be:

2

Example:

In this example, index() method is used with optional arguments to find out the index number of first occurrence of 10 in the specified tuple.

MyTuple = [10, 20, 30, 40, 50, 10, 70, 80]

#first occurrence of 10 in the whole tuple
x = MyTuple.index(10) 
print("The index of 10 is:", x)

#first occurrence of 10 in the specified
#section of the tuple
y = MyTuple.index(10, 1, 8) 
print("The index of 10 in index range [1,8) is:", y)

The output of the above code will be:

The index of 10 is: 0
The index of 10 in index range [1,8) is: 5

❮ Python Tuple Methods

5