Python 3 reference · List Methods

Python List index() Method

Return the position of the first item equal to a value. Raises ValueError if the value isn't in the list.

Python 3 Since 3.0

Syntax

list.index(value, start=0, stop=len(list))

Parameters

ParameterDescription
value The value to find.
start, stop optional Only search this slice.

Return value

An int.

Examples

Find a positionTry it yourself
colors = ["red", "green", "blue", "green"]
print(colors.index("green"), colors.index("green", 2))

Output

1 3
Handle a missing valueTry it yourself
items = ["a", "b"]
print(items.index("b") if "b" in items else -1)
try:
    items.index("z")
except ValueError:
    print("not in list")

Output

1
not in list

Version history

VersionChange
Python 3.0Added