access

list.index

Returns the zero-based index of the first item equal to x within the optional [start, end) range. Raises ValueError if no match is found.

list.index(x[, start[, end]])

Parameters

Parameter Purpose
x Value to find; compared with ==.
start (optional) Index at which to begin the search.
end (optional) Index at which to stop (exclusive); like a slice, out-of-range is clamped.

Examples

>>> lst = ['a', 'b', 'c', 'b']
>>> lst.index('b')
1

Returns the first match's index.

>>> lst = ['a', 'b', 'c', 'b']
>>> lst.index('b', 2)
3

Search from index 2 onward.

>>> [1, 2, 3].index(9)
ValueError: 9 is not in list

Missing value raises ValueError — check with `in` first or use try/except.

>>> lst = [1, 2, 3, 4]
>>> lst.index(3, 0, 2)
ValueError: 3 is not in list

The end argument is exclusive; 3 is at index 2, outside [0, 2).

Gotcha

Raises ValueError when x is absent — always guard. The end argument is EXCLUSIVE, matching slice semantics.

Related methods

← All Python list methods · Dict methods · list vs tuple vs set