How to fix list index out of range in Python

IndexErrors are one of the most common types of runtime errors in Python. They’re raised when you try to access an index value inside a Python list that does not exist. In most cases, index errors are easy to resolve. We just need to do a little debugging.
>>> a = [2, 4, 2, 1, 5] >>> print(a[5]) Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list index out of range >>>
To solve the ‘IndexError: list index out of range’ error, we should make sure that we’re not trying to access a non-existent item in a list.
If you are using a loop to access an item, make sure that the loop accounts for the fact that lists are indexed from zero. If that does not solve the problem, check to make sure that you are using range() to access each item by its index value.
Subscribe
Login
Please login to comment
0 Discussion