In Python, you get the index of an element in a list with the index() method: my_list.index(value) returns the position of the first match. Positions start at 0, so the first element is at index 0, the second at index 1, and so on.
The index() method covers the common case. For repeated values, conditions, or missing values, you can use enumerate(), a list comprehension, or next() instead. This lesson shows all four ways and how to avoid the ValueError that index() raises when the value is not in the list.
How Do You Find the Index of an Element in a List in Python?
Call the index() method on the list with the value you are looking for, and it returns the position of the first match.
# 1. Make a list
fruits = ["mango", "banana", "cherry", "kiwi"]
# 2. Ask for the position of a value
print(fruits.index("cherry")) # Outputs: 2
"cherry" is the third element, and counting starts at 0, so its index is 2.
What Is an Index in Python?
An index is the numbered position of an element inside a sequence. Indexing in Python is zero-based: the first element sits at index 0. You use an index in square brackets to read the element at that position.
cities = ["london", "tokyo", "delhi", "paris"]
print(cities[0]) # Outputs: london
print(cities[2]) # Outputs: delhi
print(cities[-1]) # Outputs: paris
Negative indexes count from the end, so cities[-1] is the last element. index() does the reverse of square brackets: you give it the value and it gives you the position.
4 Ways to Get the Index of an Item in a List
1) The index() Method
The index() method scans the list from the start and returns the position of the first element equal to the value. It also accepts optional start and end positions to search only part of the list.
tasks = ["email", "review", "deploy", "review"]
print(tasks.index("review")) # Outputs: 1
print(tasks.index("review", 2)) # Outputs: 3
The second call starts searching at index 2, so it skips the first "review" and finds the one at index 3. The scan is O(n): the method checks elements one by one until it hits a match.
2) The enumerate() Function
The enumerate() function pairs each element with its index while you loop. Use it when you need the index and the value together, or when you want every match instead of the first.
scores = [72, 88, 95, 88]
for position, score in enumerate(scores):
if score == 88:
print(position)
# Outputs:
# 1
# 3
3) A List Comprehension for All Indices
A list comprehension with enumerate() collects the list of indices for every occurrence of a value in one line.
guests = ["asha", "leo", "maya", "leo", "zoe"]
positions = [i for i, name in enumerate(guests) if name == "leo"]
print(positions) # Outputs: [1, 3]
4) The next() Function with a Condition
index() only matches exact values. To find the index of the first element that satisfies a condition, pass a generator to next(). The second argument is a default returned when nothing matches.
prices = [40, 65, 120, 90]
first_expensive = next((i for i, p in enumerate(prices) if p > 100), -1)
print(first_expensive) # Outputs: 2
How to Handle a Value That Is Not in the List
When the value is missing, index() raises a ValueError instead of returning something like -1.
colors = ["red", "green", "blue"]
print(colors.index("orange"))
# ValueError: 'orange' is not in list
To get the index without an exception, test membership with in first, or catch the error.
colors = ["red", "green", "blue"]
if "orange" in colors:
print(colors.index("orange"))
else:
print("not found") # Outputs: not found
The in check and the index() call each scan the list, so this runs two passes in the worst case. A try/except ValueError block does it in one pass.
When to Use Each Method
All four approaches scan the list, so each is O(n) time. The difference is what they return.
| Method | Returns | Use it when |
|---|---|---|
list.index(value) | Index of the first match | You need one exact value |
enumerate() loop | Index and value pairs | You act on matches inside a loop |
| List comprehension | List of all matching indices | The value can appear more than once |
next() with a condition | First index passing a test, or a default | You match a condition, not a value |
Examples of Finding an Index in a List
1) Position of a User on a Leaderboard
leaderboard = ["priya_codes", "dev_marco", "sam_dev", "lena_py"]
rank = leaderboard.index("sam_dev") + 1
print(rank) # Outputs: 3
Adding 1 converts the zero-based index into a human-friendly rank.
2) Locating a Column in a Header Row
header = ["order_id", "customer", "total", "shipped"]
total_col = header.index("total")
row = ["A1043", "[email protected]", 249.99, True]
print(row[total_col]) # Outputs: 249.99
3) Every Index of a Repeated Reading
temperatures = [21.5, 23.0, 21.5, 24.2, 21.5]
cold_spots = [i for i, t in enumerate(temperatures) if t == 21.5]
print(cold_spots) # Outputs: [0, 2, 4]
Learn More About List Indexing in Python
Negative Indexing in Python
Negative indexes read from the end of the list: -1 is the last element, -2 the one before it. index() always returns a non-negative position, even if you could also reach the element with a negative index.
queue = ["anna", "raj", "sofia"]
print(queue[-1]) # Outputs: sofia
print(queue.index("sofia")) # Outputs: 2
Find a String Position in a List
String elements work exactly like any other value: to find a string position in a list, pass the string to index(). Matching is case-sensitive, so "Pending" and "pending" are different values.
statuses = ["shipped", "pending", "delivered"]
print(statuses.index("pending")) # Outputs: 1
print("Pending" in statuses) # Outputs: False
The indexOf Equivalent in Python
Python has no indexOf() function. Coming from JavaScript or Java, the equivalent of indexOf is the index() method, with one difference: indexOf returns -1 for a missing value, while index() raises a ValueError. Use the next() pattern with a default of -1 to reproduce the indexOf behavior.
Finding an Index in a NumPy Array
NumPy arrays have no index() method. Use np.where() to get the indices of matching elements in an array.
import numpy as np
readings = np.array([12, 45, 12, 78])
print(np.where(readings == 12)[0]) # Outputs: [0 2]
Looping with an Index and a Value
To print each index next to its element, loop with enumerate(). Passing start=1 makes the count begin at 1 instead of 0.
steps = ["clone the repo", "install packages", "run the tests"]
for number, step in enumerate(steps, start=1):
print(number, step)
# Outputs:
# 1 clone the repo
# 2 install packages
# 3 run the tests
Key Takeaways for Getting a List Index
- index() - returns the position of the first exact match and raises
ValueErrorif the value is missing. - Zero-based - the first element is at index 0, and negative indexes count from the end.
- All occurrences - a list comprehension with
enumerate()returns every matching index, not just the first. - Conditions -
next()with a generator finds the first index that passes a test and can return a default such as -1. - Cost - every method scans the list, so lookups by value are O(n) time.
Lists keep their elements in a fixed order, which is what makes index positions meaningful. To see how an ordered collection behaves when it cannot be changed at all, read our lesson on converting a list to a tuple in Python.

By Komal Gupta 