index(value) searches from left to right and returns the position (counting from 0) of the first item equal to value. The list is not changed.
If nothing matches it raises ValueError; lists have no find() that returns -1. Optional start and end arguments limit the search like a slice, but the number you get back still counts from the start of the whole list.
list.index() is an instance method of the built-in list type: call it on a list with dot notation, such as queue.index(...). No import is needed.
Syntax
Pass the value, and optionally where to start and stop looking.
Returns 2: the search starts at 1, but the position counts from the list's start.
["a", "b"].index("z")
Raises ValueError: no item equals "z".
The arguments are positional only: lst.index(x, start=2) raises TypeError. Negative start and end values count from the end, just like slice bounds.
Method signatures
python
list.index(value, start=0, end=len(list))
# returns: int, the position of the first item == value in list[start:end]
# raises: ValueError when there is no such item
Parameters
Parameter
Description
value
The value to find, compared with ==, so 1.0 finds an item 1.
start
Optional. Position where the search begins (default 0). Negative values count from the end.
end
Optional. The search stops before this position (default: the end of the list).
Return Value
Return Value
Description
int
The position of the first match, from 0 to len(lst) - 1, always measured from the start of the list.
Python Examples
Example 1 — Tell a Customer Their Place in Line
Turn a zero-based index into a human position, and check membership before calling index().
python
queue = ["Maya", "Leo", "Iris", "Omar"]
pos = queue.index("Iris")
print(pos)
print(f"Iris is number {pos + 1} in line")
name = "Zane"
if name in queue:
print(queue.index(name))
else:
print(name, "is not in the queue")
Output
2
Iris is number 3 in line
Zane is not in the queue
How It Works
Zero-based answer. Iris is the third name, so index() returns 2.
Human numbering. Adding 1 turns the index into the place a person would say out loud.
Look before you leap.name in queue is False for Zane, so index() is never called and no ValueError can happen.
Example 2 — Find Repeated Entries in a Race Lap Log
Use start to find the second match, and start plus end to search only part of the list.
python
laps = ["pit", "fast", "slow", "fast", "pit", "fast"]
first = laps.index("fast")
second = laps.index("fast", first + 1)
print(first, second)
print(laps.index("pit", 1))
print(laps.index("fast", 2, 4))
try:
laps.index("slow", 3)
except ValueError:
print("no 'slow' lap from index 3 on")
Output
1 3
4
3
no 'slow' lap from index 3 on
How It Works
Next match after the first. The first "fast" is at 1, so searching again from 2 finds the next one at 3.
Absolute positions. Starting at 1 skips the pit stop at 0 and finds the next one; the answer is 4, counted from the beginning, not from the start argument.
A window.index("fast", 2, 4) looks only at positions 2 and 3 and finds the match at 3.
Outside the window. The only "slow" lap is at index 2, before the start of the search, so ValueError is raised and caught.
Example 3 — Locate the Hottest Days in a Week of Temperatures
Wrap index() in a helper that returns -1, then collect every position with enumerate().
python
def find(items, target):
try:
return items.index(target)
except ValueError:
return -1
temps = [18, 21, 25, 21, 19, 25]
print(find(temps, 25), find(temps, 30))
hot_days = [day for day, t in enumerate(temps) if t == 25]
print(hot_days)
print(temps.index(max(temps)))
Output
2 -1
[2, 5]
2
How It Works
A find() of your own. The helper turns the ValueError for 30 into -1, like str.find() does for strings.
First match only. 25 appears twice, but index() reports just the first, at 2.
Every position. The comprehension keeps each index from enumerate() whose temperature is 25, giving 2 and 5.
Position of the maximum.max() finds the highest value, and index() says where it first occurs.
Frequently Asked Questions
It does not return anything: it raises ValueError. Check with value in lst first, or use try/except ValueError if you want a fallback such as -1.
Use a comprehension with enumerate(): [i for i, x in enumerate(lst) if x == value]. index() only gives the first one.
No. start only decides where the search begins; the returned position is always counted from the beginning of the whole list.
Search the reversed list and convert back: len(lst) - 1 - lst[::-1].index(value). It raises ValueError too if the value is missing.
No. list.index() only accepts positional arguments, so write lst.index(x, 2, 5). Using start=2 raises TypeError.