Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use items.index(target) to get the zero-based index of a value’s first occurrence in a Python list. To get every matching index, use enumerate() in a list comprehension. If the value is missing, list.index() raises ValueError.

Get the first matching index with list.index()

Call index() on the list and pass the value to find:

items = ["red", "blue", "red"]
target = "red"

first_index = items.index(target)
print(first_index)  # 0

The result is zero-based, so the first item is at index 0. Python’s official tutorial describes list.index(value) as returning the zero-based index of the first occurrence of the value.

Handle a value that might be absent

If no equal value is in the list, items.index(target) raises ValueError. When a missing value is an expected outcome, catch that exception:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
try:
    position = items.index(target)
except ValueError:
    position = None

Alternatively, check membership before calling index():

if target in items:
    position = items.index(target)
else:
    position = None

The membership check is straightforward, but a successful lookup searches the list twice: once for in and again for index(). Catching ValueError avoids that repeated search.

Find every matching index

list.index() returns only the first position. To collect all positions, use enumerate() to pair each value with its index, then keep the indexes for values equal to the target:

items = ["red", "blue", "red"]
target = "red"

all_indexes = [i for i, value in enumerate(items) if value == target]
print(all_indexes)  # [0, 2]

If there are no matches, the comprehension returns an empty list, []. The Python tutorial documents enumerate() for retrieving a sequence’s position and corresponding value.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Limit the search with start and stop

You can pass optional bounds to index():

items.index(target, start, stop)

The search covers the slice-like range beginning at start and ending before stop. The returned index is still measured from the beginning of the original list, not from start. To search for a later occurrence, start after the previous match. For finding an unknown number of matches, the enumerate() comprehension is generally clearer than repeatedly calling index().

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose the method for the result you need

Approach What it returns When no value matches
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 values, not their positions Returns 0

Use count() when you only need to know how many occurrences there are; use enumerate() when you need their locations. Python documents index(), count(), and the other list methods in its list reference.

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.