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() method 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: 1output
1If 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 tupleoutput
Traceback (most recent call last):
File "main.py", line 2, in <module>
result = my_tuple.index(123)
ValueError: tuple.index(x): x not in tupleIf 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: 1output
1Example 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"
result = my_tuple.index(element)
print(result) # Output: 2
result = my_tuple.index(element, 4)
print(result) # Output: 8
result = my_tuple.index(element, 4, 6)
print(result) # Raises ValueError: tuple.index(x): x not in tupleoutput
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 tupleExample 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.