tuple.index()
The other half of the two-method tuple interface. Unlike str.find there is no -1 variant — absence is always an exception.
Demo
index scans left to right and stops at the first element equal to the value, returning its position. With duplicates you always get the earliest one. A value that is not present raises ValueError — there is no tuple equivalent of str.find that would hand back -1, so absence must be handled with try or checked first with the in operator.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| value | Any | yes | Value to locate. Compared with ==, so equal-but-not-identical objects match. |
| start | int | no (0) | Where to begin searching. The returned index is still absolute. |
| stop | int | no (len) | Where to stop searching, exclusive. |
Return value
int — Lowest index whose element equals value. Raises ValueError if there is no match.
Common patterns
col = header.index("email") value = row[col]
if value in t: i = t.index(value)
second = t.index(value, t.index(value) + 1)
Examples
Pitfalls
('a', 'b').find('z')
i = t.index('z') if 'z' in t else -1
('a', 'b', 'a').index('a')
[i for i, x in enumerate(t) if x == 'a']
if v in t: i = t.index(v)
try: i = t.index(v) except ValueError: i = -1
When to use
- Mapping a header name to a column position
- Locating a known-present value in a fixed record
- Any place you would write next(i for i, x in enumerate(t) ...)
- Absence is expected and normal → test with in first
- You need every position → enumerate with a comprehension
- You want the COUNT → tuple.count
Notes
FAQ
Because -1 is a valid index in Python — it means the last element. Returning it for "not found" would make t[t.index(v)] quietly give the wrong element instead of failing. str.find can get away with -1 only because callers are expected to check.
t = ('a', 'b') # if index returned -1, t[-1] would look like a match