IndexError: list index out of range (Fix Explained)
IndexError: list index out of range means your code is trying to read a list position that does not exist.
This usually happens when:
- the index is too large
- the list is empty
- a loop goes one step too far
- you expect more items than are really in the list
The good news is that this error is usually easy to fix. In most cases, you need to check the list length with len() or use a safer looping pattern.
Quick fix
items = ["a", "b", "c"]
index = 2
if 0 <= index < len(items):
print(items[index])
else:
print("Index is out of range")
What this does:
len(items)gets the number of items in the list0 <= index < len(items)checks whether the index is valid- If the index is valid, Python reads the item
- If not, the program avoids the error
Output:
c
Use len() to make sure the index exists before reading from the list.
What this error means
A list index is a position in a list.
For example, in this list:
colors = ["red", "green", "blue"]
The indexes are:
0→"red"1→"green"2→"blue"
Python raises IndexError: list index out of range when you ask for a position that is not in the list.
Valid list indexes
For a list with length 3:
- valid positive indexes are
0,1, and2 - valid negative indexes are
-1,-2, and-3
Anything outside that range will fail.
If you need a refresher on list basics, see Python lists explained for beginners.
Simple example that causes the error
Here is a short list with 3 items:
items = ["a", "b", "c"]
print(items[3])
This will raise:
IndexError: list index out of range
Why this fails
The list has 3 items, but its valid indexes are:
012
So items[3] does not exist.
A common beginner mistake is thinking that a 3-item list has indexes 1, 2, and 3. In Python, list indexes start at 0.
Why it happens
This error can happen in several common situations.
Using an index that is too large
numbers = [10, 20, 30]
print(numbers[5])
Index 5 is outside the list.
Using a negative index that goes too far
Negative indexes count from the end:
-1is the last item-2is the second-to-last item
But this still has limits:
numbers = [10, 20, 30]
print(numbers[-4])
This fails because the list only supports negative indexes down to -3.
Looping with the wrong range() stop value
This is very common:
items = ["a", "b", "c"]
for i in range(len(items) + 1):
print(items[i])
This loop goes through:
0123
Index 3 does not exist, so the loop crashes at the end.
If you want to understand loop boundaries better, see the Python range() function explained.
Accessing a list before checking that it has items
items = []
print(items[0])
This fails because the list is empty.
Assuming input always has enough items
Sometimes the list comes from user input, split(), a file, or an API.
text = "Alice"
parts = text.split(",")
print(parts[1])
This fails because parts only has one item.
How to fix it
There are several safe ways to fix this error.
1. Check the list length with len() before indexing
items = ["a", "b", "c"]
index = 1
if 0 <= index < len(items):
print(items[index])
else:
print("Invalid index")
This is one of the clearest fixes for beginners.
2. Use the correct loop range
If you really need indexes, use:
items = ["a", "b", "c"]
for i in range(len(items)):
print(i, items[i])
Output:
0 a
1 b
2 c
Notice that the loop stops before len(items).
3. Prefer direct iteration when possible
In many cases, you do not need indexes at all.
items = ["a", "b", "c"]
for item in items:
print(item)
This is often safer and simpler.
For more help, see how to loop through a list in Python.
4. Check for an empty list before using my_list[0]
items = []
if items:
print(items[0])
else:
print("The list is empty")
In Python, an empty list is treated as False, so if items: is a simple emptiness check.
5. Print the index and list length while debugging
If you are not sure what is going wrong, print both values:
items = ["a", "b", "c"]
index = 3
print("List:", items)
print("Length:", len(items))
print("Index:", index)
if 0 <= index < len(items):
print(items[index])
else:
print("Index is out of range")
This makes it easier to see the mismatch.
Common real examples
These are common ways beginners run into this error.
Reading the first item from an empty list
results = []
if results:
print(results[0])
else:
print("No results found")
Using i + 1 near the end of a loop
items = ["a", "b", "c"]
for i in range(len(items) - 1):
print(items[i], items[i + 1])
This works because the loop stops before i + 1 goes out of range.
Splitting text and expecting more parts than exist
name = "Alice"
parts = name.split()
if len(parts) >= 2:
print(parts[1])
else:
print("There is no second word")
Accessing results from a file or API without checking length
results = ["first result"]
if len(results) >= 2:
print(results[1])
else:
print("There is no second result")
When data comes from outside your program, always check its length first.
Debugging steps
If you are getting this error and do not know why, try these steps.
1. Print the list
print(my_list)
This shows what is actually in the list.
2. Print the list length
print(len(my_list))
This tells you the highest valid positive index: len(my_list) - 1.
3. Print the index value
print(index)
Sometimes the index is not what you expected.
4. Check whether the list is empty
if not my_list:
print("The list is empty")
This is important before using my_list[0].
5. Confirm loop boundaries
If the error happens in a loop, check your range() carefully.
Bad:
for i in range(len(my_list) + 1):
print(my_list[i])
Good:
for i in range(len(my_list)):
print(my_list[i])
If you need both the index and the value, enumerate() is often a better choice.
Ways to avoid this error
These habits help prevent the problem before it happens.
Loop over items instead of indexes
items = ["a", "b", "c"]
for item in items:
print(item)
This avoids manual index mistakes.
Use enumerate() when you need both index and value
items = ["a", "b", "c"]
for i, value in enumerate(items):
print(i, value)
Output:
0 a
1 b
2 c
This is safer than managing the index yourself in many cases.
Check list length before reading fixed positions
items = ["apple", "banana"]
if len(items) > 1:
print(items[1])
Use slicing when a missing range should not crash the program
Slicing is often safer because it does not raise IndexError when the end is too large.
items = ["a", "b", "c"]
print(items[0:5])
Output:
['a', 'b', 'c']
That can be useful when you want a range of items without crashing.
Common mistakes
These are some very common causes of this error:
- Trying to access
my_list[len(my_list)] - Using
range(len(my_list) + 1) - Reading
my_list[0]when the list is empty - Using a negative index smaller than
-len(my_list) - Expecting
split()to return more items than it did
Why my_list[len(my_list)] is wrong
If a list has length 3, the valid indexes are 0, 1, and 2.
So this is wrong:
my_list = ["a", "b", "c"]
print(my_list[len(my_list)])
Because len(my_list) is 3, and index 3 is out of range.
FAQ
Why does index 3 fail in a list with 3 items?
Because list indexes start at 0. A 3-item list has valid indexes 0, 1, and 2.
Can negative indexes cause this error?
Yes. Negative indexes work only if they stay within the list length, such as -1 or -2 for a non-empty list.
How do I check if an index is valid?
Use a condition like this before accessing the item:
if 0 <= index < len(my_list):
print(my_list[index])
Should I use try-except for this?
You can, but for beginners it is often clearer to check the length first when missing items are expected.
For example:
items = ["a", "b", "c"]
index = 5
try:
print(items[index])
except IndexError:
print("Index is out of range")
This works, but if out-of-range access is a normal possibility, checking first is often easier to read.