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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
index() returns the zero-based position of a matching list item.start argument changes where the search begins.end argument limits the search and is not included.start and end.ValueError is raised.in operator can check whether a value exists before using index().try...except can be used when a missing value needs to be handled.Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.