list.index() Find element index
Last updated: 2026-09-22
list.index() Find element index
list.index(x[, start[, end]]) returns the first index of x; raises ValueError if missing.
| Category | List Methods |
|---|---|
| Kind | Built-in Type Method |
| Python Version | all |
📝 Syntax
list.index(x[, start[, end]])
⚙️ Parameters
| value Required | The element to search for. |
|---|---|
| start Optional | Start index for the search; default is from the beginning.(Default:None) |
| end Optional | End index (exclusive); default is to the end.(Default:None) |
Returns:int, the index of the first occurrence of value.
Mutates the original:No (returns a new object)
💥 Raises
ValueError— Raised when value is not in the list (or the given range).
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
1
err: 3 is not in list
💡 Related Cases
More case studies coming soon — check back later.
❓ FAQ
QWhat happens when the element is not in the list?
AA ValueError: x is not in list is raised.
QHow do I safely look up an element's position?
ACheck with in first: if x in lst: i = lst.index(x); or catch ValueError with try/except.
QHow are start/end parameters used?
AThey delimit the search range (left-closed, right-open): [10, 20, 30, 20].index(20, 2) starts searching from index 2 and returns 3.
QHow do I find the last occurrence?
ALists have no rindex(); you can search in reverse: len(lst) - 1 - lst[::-1].index(x).