Python Tuple index() Function
The Tuple index()
method finds the position of the first occurrence of the given element in the tuple.
If specified item is not found, it raises ValueError
exception.
The optional arguments start
and end
can be used to limit the search to a particular subsequence of the tuple.
Syntax
my_tuple.index(item, start, end)
index() Parameters
Python Tuple index()
method parameters:
Parameter | Condition | Description |
---|---|---|
item | Required | Any item (of type string, list, set, etc.) you want to search for |
start | Optional | An index specifying where to start the search. Default value is 0. |
end | Optional | An index specifying where to stop the search. Default value is the end of the list. |
index() Return Value
Python Tuple index()
function returns the index of the given element in the tuple.
Examples
Example 1: Get index of given Element in a Tuple
If the given element is contained in the tuple, then index()
method returns the index of the first occurrence:
my_tuple = ('Tom', 'Anna', 'David', 'Ryan')
result = my_tuple.index('Anna')
print(result) # Output: 1
output
1
If the given element is not found in the tuple, then index()
method raises ValueError
exception.
my_tuple = ('Tom', 'Anna', 'David', 'Ryan')
result = my_tuple.index(123) # Raises ValueError: tuple.index(x): x not in tuple
output
Traceback (most recent call last):
File "main.py", line 2, in <module>
result = my_tuple.index(123)
ValueError: tuple.index(x): x not in tuple
If the tuple has many instances of the specified item, the index()
method returns the index of first instance only.
my_tuple = ('Tom', 'Anna', 'David', 'Anna', 'Ryan')
result = my_tuple.index('Anna')
print(result) # Output: 1
output
1
Example 2: Get index of given Element in a Tuple with start
and end
indexes
If you want to limit the search to a sub-portion of the given tuple, you can specify the start
parameter and/or the end
parameter.
my_tuple = ('a','b','c','d','e','f','a','b','c','d','e','f')
element = "c"
# index()
result = my_tuple.index(element)
print(result) # Output: 2
# index() after 4th index
result = my_tuple.index(element, 4)
print(result) # Output: 8
# index() between 4th and 6th index
result = my_tuple.index(element, 4, 6)
print(result) # Raises ValueError: tuple.index(x): x not in tuple
output
2
8
Traceback (most recent call last):
File "main.py", line 13, in <module>
result = my_tuple.index(sub, 4, 6)
ValueError: tuple.index(x): x not in tuple
The returned index is computed relative to the beginning of the full sequence rather than the start
argument.
Example 3: Handling ValueError of index() method
You can handle the ValueError
exception using a try-except block.
my_tuple = ('Tom', 'David', 'Ryan')
element = "Anna"
try:
index = my_tuple.index(element)
print("The index where the element is found:", index)
except ValueError:
print("The element does not exist in the tuple.")
output
The substring does not exist in the tuple.
Alternatively, you can check if item exists in a tuple, using in
operator inside if
statement.
my_tuple = ('Tom', 'David', 'Ryan')
element = "Anna"
try:
index = my_tuple.index(element)
print("The index where the element is found:", index)
except ValueError:
print("The element does not exist in the tuple.")
output
The substring does not exist in the tuple.