Python List index() Method

list.index() finds the position of a value in a list.

Use it when you have a list and want to know where a specific item appears. It returns the index of the first matching item. If the value is not in the list, Python raises a ValueError.

Quick answer #

items = ["apple", "banana", "cherry"]
position = items.index("banana")
print(position)  # 1

Use list.index(value) to get the position of the first matching item. It raises ValueError if the item is not found.

apple0-3banana1-2cherry2-1[1:2]
items.index("banana") returns 1 — the position of the first match.

What list.index() does #

list.index():

  • Finds the position of a value in a list
  • Returns the index of the first matching item
  • Uses zero-based indexing, so the first item is at position 0
  • Works only if the value exists in the list

If you are new to lists, see Python lists explained for beginners.

Basic syntax #

The basic form is:

my_list.index(value)

You can also limit the search:

my_list.index(value, start, end)

Here is what each part means:

  • value: the item to search for
  • start: where the search begins
  • end: where the search stops
  • end is not included in the search range

This means Python searches from start up to, but not including, end.

What the return value means #

list.index() returns an integer.

That integer is the position of the first match in the list.

Example:

numbers = [10, 20, 30]
result = numbers.index(20)

print(result)

Output:

1

If the same value appears more than once, index() still returns only the first matching position.

Example: find an item in a list #

Here is a simple example with strings:

fruits = ["apple", "banana", "cherry"]
position = fruits.index("cherry")

print(position)

Output:

2

In this list:

  • "apple" is at index 0
  • "banana" is at index 1
  • "cherry" is at index 2

So fruits.index("cherry") returns 2.

If you want a task-focused example, see how to find an item in a list in Python.

Example: list with duplicate values #

If a list contains the same value more than once, index() returns the first match only.

colors = ["red", "blue", "red", "green"]

print(colors.index("red"))

Output:

0

Even though "red" appears again later, Python returns 0 because that is the first matching position.

If you need to count how many times a value appears, use Python list count() method.

Using start and end #

The optional start and end arguments let you search only part of a list.

letters = ["a", "b", "c", "b", "d"]

print(letters.index("b"))        # first match
print(letters.index("b", 2))     # search starting at index 2
print(letters.index("b", 1, 4))  # search from 1 up to 4

Output:

1
3
1

How this works:

  • letters.index("b") searches the whole list and returns the first "b" at index 1
  • letters.index("b", 2) starts searching at index 2, so it finds the next "b" at index 3
  • letters.index("b", 1, 4) searches indexes 1, 2, and 3

This is useful when you want to skip earlier items and find a later match.

What happens if the value is missing #

If the value is not in the list, Python raises a ValueError.

Example:

items = ["apple", "banana", "cherry"]
print(items.index("orange"))

This causes an error because "orange" is not in the list.

A safer approach is to check first:

items = ["apple", "banana", "cherry"]
search_value = "orange"

if search_value in items:
    print(items.index(search_value))
else:
    print("Item not found")

Output:

Item not found

This avoids the error and makes your code easier to understand.

If you want more help with this kind of problem, see ValueError in Python: causes and fixes.

Common beginner mistakes #

Here are some common problems when using list.index():

  • Thinking index() returns -1 if the item is missing
    Python does not return -1. It raises ValueError.

  • Confusing the item value with the item position
    my_list.index("banana") returns the position of "banana", not the value itself.

  • Using index[] instead of index()
    This is a method call, so you must use parentheses.

    Correct:

    items.index("banana")
    

    Wrong:

    items.index["banana"]
    
  • Searching with the wrong type
    "3" and 3 are different values in Python.

    numbers = [1, 2, 3]
    print("3" in numbers)  # False
    

If your code is not working, these quick checks can help:

print(my_list)
print(type(search_value))
print(search_value in my_list)
print(my_list.index(search_value))

Be careful with the last line. It will still raise an error if the value is missing.

Common causes of problems include:

  • The searched value is not in the list
  • The value exists but has a different type, such as "3" instead of 3
  • The code expects all matches, but index() returns only the first one
  • Parentheses were forgotten when calling the method

When to use index() #

Use index() when:

  • You need the position of a known value
  • You are working with a small list
  • You only need the first matching position

Do not use it when you need every matching position. In that case, a loop or list comprehension is a better choice.

FAQ #

What does list.index() return in Python? #

It returns the index of the first matching item in the list.

Does list.index() return -1 if the value is not found? #

No. It raises ValueError instead.

Can list.index() find all matching items? #

No. It returns only the first match.

Can I search only part of a list with index()? #

Yes. You can use the optional start and end arguments.

See also #

Press Esc to close