Python 3 reference · Tuple Methods
Python Tuple index() Method
Return the position of the first item equal to a value. Raises ValueError if it isn't in the tuple.
Python 3 Since 3.0
Syntax
tuple.index(value, start=0, stop=len(tuple))
Parameters
| Parameter | Description |
|---|---|
value |
The value to find. |
start, stop optional |
Only search this slice. |
Return value
An int.
Examples
days = ("mon", "tue", "wed")
print(days.index("tue"))
Output
1
t = (1, 2)
try:
t.index(9)
except ValueError:
print("not found")
Output
not found
Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
tuple.count()· Return how many times a value appears in the tuple, comparing each item with ==. Returns 0 if it never appears.list.index()· Return the position of the first item equal to a value. Raises ValueError if the value isn't in the list.- Python Tuples · tutorial