How to Find Index of Element in List in Python
What you’ll build or solve
You’ll find the position of an item in a Python list using the right approach for your case.
When this approach works best
Finding an index in a list works well when you:
Learn Python on Mimo
- Locate a value so you can update or remove it, like replacing a status in a list.
- Find where an item appears to split or slice a list, like everything after a marker.
- Search for the first item that matches a rule, like the first even number or the first file ending in
.csv.
Skip this approach if you need fast lookups in a large collection. Lists scan from left to right, so repeated searches can get slow. A dictionary or a set is often better for frequent lookups.
Prerequisites
- Python 3 installed
- You know what a list is
Step-by-step instructions
1) Get the first index with list.index()
Use list.index() when you want the first position of an exact value.
Python
colors = ["blue", "green", "purple", "green"]
i = colors.index("green")
print(i)
What to look for: .index() returns the first match only. If the value appears multiple times, you won’t get the later positions.
2) Handle missing values safely
If the item might not be in the list, index() raises ValueError. Catch it or check first.
Option A (common): try/except ValueError
Python
colors = ["blue", "green", "purple"]
try:
i = colors.index("red")
except ValueError:
i = -1
print(i)
Option B: Check with in before calling index()
Python
colors = ["blue", "green", "purple"]
if "red" in colors:
i = colors.index("red")
else:
i = -1
print(i)
What to look for: Returning -1 is a common pattern, but you can also return None if you want to make “not found” more explicit.
3) Find an index by condition
When you need a rule instead of an exact value, loop with enumerate() and stop at the first match.
Python
nums = [3, 8, 12, 7, 5]
index_even = None
for i, n in enumerate(nums):
if n % 2 == 0:
index_even = i
break
print(index_even)
What to look for: break matters. Without it, you’ll end up with the last matching index, not the first.
Examples you can copy
Example 1: Find the index, then update the value
Python
status = ["todo", "in_progress", "done"]
i = status.index("in_progress")
status[i] = "blocked"
print(status)
Example 2: Get all indexes of a value
Python
colors = ["blue", "green", "purple", "green", "green"]
indexes = [i for i, c in enumerate(colors) if c == "green"]
print(indexes)
Example 3: Find the index within a slice
This example uses index()’s optional start parameter to begin searching from a specific position.
Python
items = ["a", "b", "c", "b", "d"]
first_b = items.index("b")
second_b = items.index("b", first_b + 1)
print(first_b, second_b)
Example 4: Find the index of the first item that matches a rule
Python
names = ["", " ", "Amina", "Luka"]
first_non_empty = None
for i, name in enumerate(names):
if name.strip():
first_non_empty = i
break
print(first_non_empty)
Example 5: Find the index of the minimum or maximum value
Python
nums = [10, 4, 25, 7]
min_i = nums.index(min(nums))
max_i = nums.index(max(nums))
print(min_i, max_i)
Common mistakes and how to fix them
Mistake 1: Assuming index() returns -1 when not found
What you might do:
Python
colors = ["blue", "green", "purple"]
i = colors.index("red")
Why it breaks: index() raises ValueError if the value isn’t present.
Correct approach:
Python
colors = ["blue", "green", "purple"]
try:
i = colors.index("red")
except ValueError:
i = -1
print(i)
Mistake 2: Getting the wrong index when the value appears multiple times
What you might do:
Python
colors = ["blue", "green", "purple", "green"]
i = colors.index("green")
Why it breaks: index() returns the first match, not the last or all matches.
Correct approach (all matches):
Python
colors = ["blue", "green", "purple", "green"]
indexes = [i for i, c in enumerate(colors) if c == "green"]
print(indexes)
Correct approach (next match):
Python
colors = ["blue", "green", "purple", "green"]
first = colors.index("green")
second = colors.index("green", first + 1)
print(first, second)
Mistake 3: Forgetting break when searching by condition
What you might do:
Python
nums = [3, 8, 12, 7, 5]
index_even = None
for i, n in enumerate(nums):
if n % 2 == 0:
index_even = i
Why it breaks: The loop keeps going, so you end up with the last matching index, not the first.
Correct approach:
Python
nums = [3, 8, 12, 7, 5]
index_even = None
for i, n in enumerate(nums):
if n % 2 == 0:
index_even = i
break
print(index_even)
Troubleshooting
If you see ValueError: x is not in list, the value isn’t present. Wrap index() in try/except or check with in first.
If your code returns the wrong index, confirm whether you wanted the first match, the last match, or all matches.
If a condition-based search returns None, no element matched the condition. Print the list and the condition to verify your expectations.
If performance is slow, avoid repeated list.index() calls in large lists. Consider building a dictionary that maps values to indexes.
Quick recap
- Use
my_list.index(value)for the first exact match. - Handle missing values with
try/except ValueErrororif value in my_list. - Use
enumerate()to find an index by condition. - Use a comprehension with
enumerate()to get all matching indexes. - Use the
startargument to find the next occurrence.
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot