IndexError is one of the many standard exception message that new Python developers will definitely encounter. It is raised when you're trying to access a value from a Python list, tuples, strings or any indexable object using a non-existent index value.
Most of the time, IndexError: List Index Out of Range is easy to fix with just a bit debugging. In this article, we’re going to talk about the IndexError: List Index Out of Range message, how it works and walk through an example scenario where this error is present so that we can solve it.
IndexError: List Index Out of Range error message
The error message often says for itself, and this is exactly the case here. "IndexError: List Index Out of Range" simply indicates that you tried to access invalid indices in a Python list. In other words, you're using a non-existent index to get the value from Python array.
Let's look an example, below is a typical Python list :
my_list = ['p','r','o','b','e']
This list contains five objects. In Python, index numbers start from 0, which means
'p' has index number 0,
'r' has index number 1,
'o' has index number 2, and so forth. The last element,
'e' has index number 4.
If you try to access an item at index number 5 or larger, the IndexError message would be raised, telling you that the compiler cannot find any element in that position.
IndexError: string index out of range with Python strings
The error can occur with strings as well, but the message would say IndexError: string index out of range. In fact, this kind of error happens with any kind of Python object that supports accessing by indices, including strings, tuples, lists, etc.
Code language: PHP (php)
s = 'Python' print(s)
IndexError: List Index Out of Range Fix
At this point, you should be able to fix the error message by simply changing the index number to the right value. If you haven't figure it out, maybe the Python list has too many elements to manually count, or you're iterating over the list and modifying it at the same time.
Looping a long list
In case of looping through a long Python list, you should use
len() to get the exact length of it before doing anything else based on indexing.
Since Python list indices start at 0, the maximum index value should be
len(the_list)-1. For example, if the list has 4 elements, the maximum valid index number is 3. If you're using
range(x, y), keep in mind that it only counts from
y out of the range.
Code language: PHP (php)
my_list = ['p','r','o','b','e'] list_length = len(my_list) for i in range(0, list_length): print(my_list[i])
Please note that this is a minimal code snippet that serves only as an example and it doesn’t make a lot of sense. But the debugging steps remains the same for bigger code projects:
- Find the index value of the loop just before the error is thrown.
- Eliminate the source of the faulty index.
IndexError when iterating and modifying lists at the same time
Python beginners often encounter "IndexError: List Index Out of Range" when they're trying to iterate over a list and modifying it at the same time. The modification can change the list length after each loop, therefore changes the index numbers dynamically.
l=[1,2,3,0,0,1] for i in range(0, len(l)): if l[i]==0: l.pop(i)
The problem in the example above comes from the
list.pop() method, which removes the element with value 0. The last loop access the index number 5, but since one or more elements are removed in the previous loops, index number 5 is now an invalid one.
You can simply fix this with list comprehension syntax that accomplishes the same thing (filter out zeros in the list):
l = [x for x in l if x]
We hope that the information above is useful to you. You may be interested in our guide on fixing “pip: command not found” error, “[Errno 32] Broken pipe” in Python and fix “Shadows name from outer scope” in PyCharm. In case you're using Ubuntu and struggling with installing Python3 over Python2, see our post on python-is-python3 package.