Use items.index(target) to get the zero-based index of the first matching element in a Python list. If you need every matching position, use enumerate() in a list comprehension instead.
Get the first matching index with list.index()
Call .index(value) on the list. Python returns the position of the first element equal to that value, counting from zero:
As an Amazon Associate I earn from qualifying purchases.
items = ["red", "blue", "red"]
target = "red"
first_index = items.index(target)
print(first_index) # 0
In the Python 3.14.8 tutorial, the method is described as returning the “zero-based index of the first occurrence of value in the list.” Python documentation: Data Structures.
Handle a value that is not in the list
If no element equals the requested value, list.index() raises ValueError. Catch that exception when a missing value is an expected outcome:
#1 Best Overall
try:
first_index = items.index(target)
except ValueError:
first_index = None
You can also test membership before looking up the index:
if target in items:
first_index = items.index(target)
else:
first_index = None
This second approach searches the list twice when the value is present: once for in and again for index(). Catching ValueError avoids that repeated search.
Rank #2
Find every matching index
list.index() stops at the first match. To collect all matching positions, pair each value with its position using enumerate(), then keep the positions whose values equal the target:
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →items = ["red", "blue", "red"]
target = "red"
all_indexes = [i for i, value in enumerate(items) if value == target]
print(all_indexes) # [0, 2]
If nothing matches, the comprehension returns an empty list, []. This is often clearer than repeatedly calling index() when you do not know how many matches to expect. Python’s tutorial documents enumerate() for retrieving a sequence’s position and corresponding value: Python documentation: Data Structures.
Limit the search with start and stop
items.index(target, start, stop) searches from start up to, but not including, stop. These bounds restrict which list elements are searched; if a match is found, its result is still an index in the original list.
items = ["red", "blue", "red"]
later_index = items.index("red", 1, 3)
print(later_index) # 2
Here the search begins at position 1, so the match at position 0 is skipped. The returned 2 is not an offset relative to the search start. The official tutorial also demonstrates using a starting position to search for a later occurrence: Python documentation: Data Structures.
Choose the method that matches the result you need
| Approach | What it returns | When there is no match |
|---|---|---|
items.index(target) |
The first matching index | Raises ValueError |
[i for i, value in enumerate(items) if value == target] |
A list of all matching indexes | Returns [] |
items.count(target) |
The number of matching elements, not their indexes | Returns 0 |
Use count() only when you need the number of matches, not their positions. See the Python documentation for list methods.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




