Python List index() Method


The Python index() method returns the position of the first matching item in a list.

Python lists use zero-based indexing, so the first item is at index 0, the second item is at index 1, and so on.

If the value occurs more than once, index() returns the position of the first match within the selected search range.

Syntax of index() 🔝

my_list.index(value, start, end)

The arguments are:

  • value - the item to search for.
  • start - optional starting index for the search.
  • end - optional ending index. The item at this index is not included in the search.

Only value is required.

Finding the Position of an Item 🔝

my_list=['a', 'b', 'c', 'b', 'a', 'd']

print(my_list.index('a'))
print(my_list.index('b'))
print(my_list.index('d'))
Output
0
1
5

The first 'a' is at index 0, the first 'b' is at index 1, and 'd' is at index 5.

index() Returns the First Match 🔝

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

my_list=['Alex', 'Ravi', 'John', 'Ravi']

position=my_list.index('Ravi')

print(position)
Output
1

Although 'Ravi' also appears at index 3, index() stops after finding the first match.

Use the count() method when you need to know how many times a value occurs.

Using the start Position 🔝

The optional start argument tells Python where to begin searching.

my_list=['a', 'b', 'c', 'b', 'a', 'd', 'a']

position=my_list.index('a', 2)

print(position)
Output
4

The search begins at index 2. Therefore, the 'a' at index 0 is ignored, and the next matching value is found at index 4.

Using start and end Positions 🔝

Both start and end can be supplied to search only part of a list.

my_list=['a', 'b', 'c', 'b', 'a', 'd']

position=my_list.index('b', 2, 5)

print(position)
Output
3

The search starts at index 2 and stops before index 5. The matching 'b' is found at index 3.

The end Position Is Not Included 🔝

Like list slicing, the end position is excluded from the search.

my_list=['a', 'b', 'c', 'd']

position=my_list.index('c', 0, 3)

print(position)
Output
2

Indexes 0, 1, and 2 are searched. Index 3 is not included.

Using Negative start and end Positions 🔝

Negative indexes can also be used for the optional search range.

my_list=['a', 'b', 'c', 'b', 'd']

position=my_list.index('b', -2)

print(position)
Output
3

The start value -2 refers to the second item from the end, which is index 3. The search therefore finds 'b' at index 3.

The returned value is still the normal zero-based index from the beginning of the list.

ValueError When an Item Is Not Found 🔝

If the requested value does not exist in the selected search range, index() raises a ValueError.

my_list=['a', 'b', 'c', 'b', 'a']

print(my_list.index('b', 4))
Sample Error
ValueError: 'b' is not in list

The value 'b' exists earlier in the list, but there is no 'b' at or after index 4.

Checking with in Before Using index() 🔝

When a value may not exist, we can use the in operator before calling index().

my_list=['Alex', 'Ronald', 'John']
name='Ravi'

if name in my_list:
    print(my_list.index(name))
else:
    print("Name not found")
Output
Name not found

This prevents index() from being called when the value is absent.

Handling ValueError with try...except 🔝

We can also handle a missing value using exception handling.

my_list=['Alex', 'Ronald', 'John']

try:
    position=my_list.index('Ravi')
    print(position)

except ValueError:
    print("Ravi is not in the list")
Output
Ravi is not in the list

The except block runs when index() cannot find the requested item.

Finding All Matching Positions 🔝

The index() method returns only the first matching position. To collect every matching index, we can use list comprehension with enumerate().

my_list=['a', 'b', 'c', 'b', 'a', 'b']

positions=[
    i
    for i,value in enumerate(my_list)
    if value == 'b'
]

print(positions)
Output
[1, 3, 5]

Here, all positions containing 'b' are collected rather than stopping at the first match.

Summary of index() 🔝

  • index() returns the zero-based position of a matching list item.
  • If duplicate values exist, the first matching position is returned.
  • The optional start argument changes where the search begins.
  • The optional end argument limits the search and is not included.
  • Negative values can be used for start and end.
  • The returned index is still measured from the beginning of the list.
  • If no matching item exists in the selected range, ValueError is raised.
  • The in operator can check whether a value exists before using index().
  • try...except can be used when a missing value needs to be handled.
  • Use a loop or list comprehension when all matching positions are required.
All List Methods count() pop() Questions with Solutions on List




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer