Python Function Reference

list.index

Find the first item of the list that equals x.

Python 3.13
list.index(x[, start[, end]])
  • [ ]optional part
x
The value to search for.
Any
start
The index to start searching from, 0 by default; a negative one counts from the end.
intOptional
end
The index before which the search stops, the end by default; a negative one counts from the end.
intOptional

Return value

The index of the first matching item; raises ValueError if there is none.

Examples

Position of an item in a list

Python 3.13
print([1, 2, 3].index(2))
1

Searching from a given position

Python 3.13
a = ['a', 'b', 'a']
print(a.index('a', 1))
2

Details

If the item may be missing, check for it first: x in a.

See also

Where to learn