How to find the index of a list item in Python
Use the list's built-in .index() method: items.index(value). It returns the position of the first matching item, and raises ValueError if the value isn't in the list.
Lists are ordered, so every item has a position — its index, counting from 0. The built-in .index() answers "where is this value?" in one call; the wrinkles are what happens when the value is missing and how to get every position instead of just the first. Each example runs on this page — hit Run, then edit the code and run it again.
1list.index()Recommended
items.index(value) scans the list left to right and returns the index of the first item equal to value. Indexing starts at 0, so the third item is at index 2.
Output
Only the first match is returned — "banana" appears twice but .index() gives 1. To search from a later position, pass a start index: items.index("banana", 2) returns 3.
2Handle a missing item safely
The catch with .index() is that a value that isn't there raises ValueError and stops your program. Guard it first with the in operator, returning a sentinel like -1 when the item is absent:
Output
The in check keeps things readable for a single lookup. In a tight loop where the item is usually present, a try / except ValueError around items.index(target) avoids scanning the list twice.
3Find every matching index with enumerate()
.index() stops at the first hit. When a value can repeat and you want all of its positions, pair enumerate() — which yields (index, item) pairs — with a comprehension that keeps the indices that match:
Output
enumerate() is the idiomatic way to loop with an index in Python — reach for it instead of range(len(items)) whenever you need both the position and the item.
4Which should you use?
| Method | Returns | If missing | Best for |
|---|---|---|---|
| items.index(x) | First index | Raises ValueError | Value you know is present |
| in + .index() / try-except | First index | Your default (-1) | Value might be missing |
| enumerate() comprehension | All indices | Empty list | Repeated values |
5Common variation: find the first item matching a condition
Sometimes you don't know the exact value — you want the first item that satisfies a condition (first number over 10, first name starting with "A"). Feed an enumerate() generator to next() with a default so a no-match returns gracefully instead of raising:
Output
next() pulls just the first match and stops — it doesn't scan the rest of the list. The -1 second argument is the fallback; drop it and an empty match raises StopIteration.
Frequently asked questions
What does list.index() do if the item isn't found?
It raises ValueError (the message is along the lines of x not in list) and stops execution. Guard it by checking if x in items first, or wrap the call in try / except ValueError and return a default like -1 — see the missing-item section above.
How do I find all the indexes of an item, not just the first?
list.index() only returns the first match. Use an enumerate() comprehension: [i for i, x in enumerate(items) if x == target] gives every index where the value appears (an empty list if none do).
How do I find the next occurrence after a given position?
Pass a start index as the second argument: items.index(value, start) begins the search at start. So after finding a match at index i, items.index(value, i + 1) finds the next one.
Can I find an index by a condition instead of an exact value?
Yes. Combine enumerate() with next(): next((i for i, x in enumerate(items) if CONDITION), -1) returns the index of the first item that matches, or -1 if none do. See the variation above.
Does .index() work on strings and tuples too?
Yes. Both str and tuple have an .index() method with the same behaviour — "hello".index("l") returns 2. On strings you can also use .find(), which returns -1 instead of raising when the substring is absent.