Find the Index of an Item in a List in Python
In Python, the index()
method allows you to find the index of an item in a list.
- How to use the
index()
method of a list - Implement a function like the
find()
method (returns-1
for non-existent values) - Get all indices of duplicate items
- Specify the search range for the
index()
method - Use the
index()
method with tuples - Practical example: Find the index of the maximum/minimum value
How to use the index()
method of a list
To find the index of an item in a list, specify the desired item as an argument to the index()
method. This method returns the zero-based index of the item.
l = [30, 50, 10, 40, 20]
print(l.index(30))
# 0
print(l.index(20))
# 4
If the specified item is not present in the list, the index()
method will throw a ValueError
.
# print(l.index(100))
# ValueError: 100 is not in list
Implement a function like the find()
method (returns -1
for non-existent values)
String objects (str
) have a find()
method that returns -1
if a substring is not present. However, no such method exists for lists (as of Python 3.11).
To create a function that emulates the behavior of the find()
method for lists, use the in
operator to check if an item is in the list.
def my_find(l, x):
if x in l:
return l.index(x)
else:
return -1
l = [30, 50, 10, 40, 20]
print(my_find(l, 30))
# 0
print(my_find(l, 100))
# -1
You can write it more simply using a ternary operator.
def my_find2(l, x):
return l.index(x) if x in l else -1
print(my_find2(l, 30))
# 0
print(my_find2(l, 100))
# -1
Get all indices of duplicate items
The index()
method will only return the index of the first instance of an item if it appears multiple times in a list.
l = [10, 30, 10, 10, 20, 20]
print(l.index(10))
# 0
print(l.index(20))
# 4
To get all indices of a specific item, utilize the enumerate()
function in conjunction with list comprehension.
This operation returns an empty list if the item does not exist in the list.
print([i for i, x in enumerate(l) if x == 10])
# [0, 2, 3]
print([i for i, x in enumerate(l) if x == 20])
# [4, 5]
print([i for i, x in enumerate(l) if x == 30])
# [1]
print([i for i, x in enumerate(l) if x == 100])
# []
The process can be defined as a function as follows:
def my_index_multi(l, x):
return [i for i, _x in enumerate(l) if _x == x]
print(my_index_multi(l, 10))
# [0, 2, 3]
Refer to the following articles if you want to remove or extract duplicate elements from a list.
Specify the search range for the index()
method
The index()
method supports optional second and third arguments i
and j
, allowing you to specify a search range from the i
th to j
th elements (with j
exclusive). If j
is not provided, the search continues to the end of the list.
Regardless of the range specified, the returned index is relative to the start of the full list.
l = [10, 30, 10, 10, 20, 20]
print(l.index(10))
# 0
print(l.index(10, 2))
# 2
# print(l.index(20, 2, 4))
# ValueError: 20 is not in list
print(l.index(20, 2, 5))
# 4
While l.index(x, i, j)
and l[i:j].index(x)
target the same range, the index returned when using slices is relative to the start of the slice.
print(l[2:])
# [10, 10, 20, 20]
print(l[2:].index(10))
# 0
print(l[2:5])
# [10, 10, 20]
print(l[2:5].index(20))
# 2
Use the index()
method with tuples
Like lists, tuples also have the index()
method.
t = ('c', 'a', 'a', 'b', 'c')
print(t.index('a'))
# 1
# print(t.index('x'))
# ValueError: tuple.index(x): x not in tuple
The function defined above can be directly applied to tuples as well.
print(my_find(t, 'a'))
# 1
print(my_find(t, 'x'))
# -1
print(my_index_multi(t, 'a'))
# [1, 2]
print(my_index_multi(t, 'x'))
# []
Practical example: Find the index of the maximum/minimum value
As a practical example, find the index of the maximum or minimum value in a list.
You can find the index of the maximum or minimum value in a list by passing the result of max()
or min()
to the index()
method.
You can also use the function defined above.
l = [10, 30, 10, 10, 20, 20]
print(l.index(max(l)))
# 1
print(l.index(min(l)))
# 0
print(my_index_multi(l, max(l)))
# [1]
print(my_index_multi(l, min(l)))
# [0, 2, 3]