list.index()

Where is this value? First matching index — or ValueError when absent.

List methodPython 2.0+Live demo
Common call
names.index("alice")
Returns
int — first match only
Replaces
unlike str.find there is NO -1 form for lists
Watch out
raises ValueError when absent — guard with "in" first
list.index(valuevalueThe value to locate, compared with ==.type: Any · required, startstartBegin searching at this index.type: int · default: 0=0, endendStop searching before this index.type: int · default: len(lst)=len(lst))
int

Demo

Live evaluation
Try:
Inputs
listlistcomma-separated items
valueAnyvalue to locate
startintsearch from
Output
['a', 'b', 'c', 'b'].index('b')
1

Returns the FIRST index whose item equals the value. A missing value raises ValueError — Python has no -1 convention for lists. The optional start skips earlier matches.

Parameters

NameTypeRequiredDescription
valueAnyyesThe value to locate, compared with ==.
startintno (0)Begin searching at this index.
endintno (len(lst))Stop searching before this index.

Return value

intThe index of the first item equal (==) to value. Raises ValueError when no item matches.

Common patterns

Guarded lookup
Check membership first when absence is a normal case.
if name in names:
    pos = names.index(name)
Find all positions
enumerate + comprehension beats repeated index calls.
[i for i, x in enumerate(lst) if x == value]
Next occurrence after a known one
Feed the previous match + 1 as start.
second = lst.index(value, first + 1)

Examples

1. First matching index
["a", "b", "c", "b"].index("b")
Returns
1
2. Search from an index
["a", "b", "c", "b"].index("b", 2)
Returns
3
3. Numbers compare by ==
[1, True, 2].index(True)
Returns
1

Pitfalls

1. Missing value raises
No -1 sentinel — either guard or catch.
Raises
["a", "b"].index("z")
ValueError: 'z' is not in list
Guard
pos = lst.index(v) if v in lst else None
None (guarded)
2. Only the first match
Duplicates after the first are invisible to index.
Where are the rest?
["b", "x", "b"].index("b")
0 — only the first
All positions
[i for i, x in enumerate(lst) if x == "b"]
[0, 2]
3. O(n) inside a loop
index scans from the front every call — quadratic when used per item.
Slow
for x in lst:
    i = lst.index(x)  # rescans every time
O(n²) total
Fix
for i, x in enumerate(lst):
    ...
O(n) — index comes free

When to use

Use it
  • Position of a value you know (or require) to be present
  • Resuming a search with start after a previous match
Reach for something else
  • Absence is normal → guard with "in" or catch ValueError
  • All positions → enumerate comprehension
  • Position while iterating → enumerate
  • Sorted data → bisect (binary search)

Notes

Complexity
O(n) — linear scan
Return
int; list unchanged
CPython impl
Objects/listobject.c :: list_index_impl
Memory
No allocation
Thread-safe
Reading is safe; concurrent mutation is not

FAQ

The API predates that convention; the idiomatic guard is membership first ("v in lst"), which reads better than a sentinel anyway.

History

2.0
Core list method, unchanged semantics since.