ReferenceList methods

Python List index() Method

Short answer
list.index(value, start, stop) returns the position of the first item equal to value, and raises ValueError if the value is not found.

index() answers the question "where is this value in my list?". It is the reverse of indexing with my_list[i], which gives you the value at a known position. Use it to locate a name in a roster, find where a threshold is crossed or to feed a position into pop() or insert().

The search runs from left to right and stops at the first match, so duplicates later in the list are ignored unless you start searching after the first one.

Quick facts
Syntaxmy_list.index(value) or my_list.index(value, start, stop)
Parametersvalue: item to find; start and stop (optional): limit the search to my_list[start:stop]
ReturnsThe zero-based index of the first match (relative to the whole list)
RaisesValueError if the value is not found
Time complexityO(n)

How list.index() finds the first match

Python compares each element with value using == and returns the index of the first one that is equal. The index counts from 0 and, even when you pass start, it is still measured from the beginning of the whole list.

Python
names = ["Asha", "Dev", "Meera", "Dev"]
print(names.index("Dev"))
print(names.index("Dev", 2))

Handling ValueError when the value is missing

Unlike str.find(), which returns -1 for no match, list index() raises a ValueError. Guard the call with in or catch the error. Do not use -1 as a sentinel, because -1 is also a valid index meaning the last item.

Finding every position of a value

index() only reports the first match. To get all positions, use enumerate() in a comprehension: [i for i, x in enumerate(items) if x == value]. If you only need how many matches exist, use list.count(). Searching is O(n), so for frequent lookups in a big collection consider a dictionary that maps value to position.

Examples you can run

Press Run, then edit the code and run it again.

Find a position and use it

Locate a name and print its place in the list.

Find a position and use it
ranks = ["Zoya", "Asha", "Ravi"]
pos = ranks.index("Asha")
print(pos)
print(ranks[pos])
Expected output1
Asha

Search inside a range

start and stop limit where Python looks.

Search inside a range
temps = [20, 25, 30, 25, 18]
print(temps.index(25))
print(temps.index(25, 2))
print(temps.index(25, 0, 3))
Expected output1
3
1

All positions with enumerate

Collect every index whose value matches.

All positions with enumerate
marks = [5, 8, 5, 9, 5]
where = [i for i, m in enumerate(marks) if m == 5]
print(where)
Expected output[0, 2, 4]

Common mistakes

Searching for a missing value

✗ Wrong

fruits = ["apple", "kiwi"]
print(fruits.index("mango"))
ValueError: 'mango' is not in list

✓ Right

fruits = ["apple", "kiwi"]
if "mango" in fruits:
    print(fruits.index("mango"))
else:
    print("not found")

Check with in first or catch ValueError.

Expecting every match

✗ Wrong

nums = [3, 7, 3]
print(nums.index(3))
0 (only the first position is returned)

✓ Right

nums = [3, 7, 3]
print([i for i, n in enumerate(nums) if n == 3])

Use [i for i, n in enumerate(nums) if n == 3] to get all positions.

Treating -1 as not found

✗ Wrong

items = ["a", "b"]
try:
    pos = items.index("z")
except ValueError:
    pos = -1
print(items[pos])
b (-1 is a valid index: the last item)

✓ Right

items = ["a", "b"]
try:
    pos = items.index("z")
except ValueError:
    pos = None
print(pos)

Use None or a separate flag for not found, and check it before indexing.

Python List index() Method: questions people ask

How do I find the index of an element in a list in Python?

Use my_list.index(value). It returns the position of the first match and raises ValueError if the value is absent.

What does index() return if the item is not in the list?

It does not return anything; it raises ValueError. Test with value in my_list first or use try/except.

How do I get the indexes of all occurrences of a value?

Use [i for i, x in enumerate(my_list) if x == value], because index() only finds the first one.

Keep learning

Step-by-step lessons:
Python ListsPython List Methods

Related:
Python List count() MethodPython List remove() MethodPython List insert() MethodPython Tuple index() Method

More in list methods:
Python List append() MethodPython List extend() MethodPython List insert() MethodPython List remove() MethodPython List pop() MethodPython List clear() MethodPython List count() MethodPython List sort() Method See all →