Python IndexError: list index out of range
IndexError: list index out of range
You get this error when you try to read or change an item at an index the list does not have. A list of length 3 only has the indexes 0, 1 and 2, so there is no a[3]. On the line that the line number in the error message points to, check that the number inside the square brackets is smaller than the length of the list.
Cause 1. Indexes start at 0
The index of the last item is not the length but length - 1. With three values, the last one is a[2].
Wrong code
a = [10, 20, 30]print(a[3])Traceback (most recent call last): File "main.py", line 2, in <module> IndexError: list index out of range
Fixed code
Use a[-1] to get the last value without knowing the length. Written as an index, it is a[len(a) - 1].
Cause 2. The list is empty
An empty list does not even have a[0]. This often happens when there is not a single line of input, or when no value matches a condition and the list ends up empty.
Wrong code
scores = [50, 40, 30]passed = [s for s in scores if s >= 60]print(passed[0])Traceback (most recent call last): File "main.py", line 3, in <module> IndexError: list index out of range
Fixed code
Ask whether the list is empty before you take a value out. An empty list counts as false in an if.
scores = [50, 40, 30]passed = [s for s in scores if s >= 60]if passed: print(passed[0])else: print("none")none
Cause 3. Looking at i + 1 in a loop
If you use a[i + 1] to compare an item with the next one, i + 1 is outside the list when i is the last index. The earlier values print fine, and then the error comes on the last pass.
Wrong code
a = [3, 5, 4, 8]for i in range(len(a)): if a[i] < a[i + 1]: print(a[i], "<", a[i + 1])3 < 5 4 < 8 Traceback (most recent call last): File "main.py", line 3, in <module> IndexError: list index out of range
Fixed code
Loop one time fewer: range(len(a) - 1). Or pair up neighbouring values with zip(a, a[1:]), and you do not need indexes at all.
a = [3, 5, 4, 8]for i in range(len(a) - 1): if a[i] < a[i + 1]: print(a[i], "<", a[i + 1])for x, y in zip(a, a[1:]): if x > y: print(x, ">", y)3 < 5 4 < 8 5 > 4
Cause 4. Deleting while you loop
range(len(a)) is fixed by the length at the moment the loop starts. If you delete values as you go, the list gets shorter but the index still climbs to the original length and goes outside the list.
Wrong code
a = [1, 2, 3, 4, 5, 6]for i in range(len(a)): if a[i] % 2 == 0: del a[i]print(a)Traceback (most recent call last): File "main.py", line 3, in <module> IndexError: list index out of range
Fixed code
Do not delete. Build a new list with only the values you want to keep.
What to check when you see this error
- The number after
linein the error message is the line where the error happened. Start with the value inside the square brackets[ ]on that line. - Put
print(len(a), i)right above that line to see the length and the index for yourself. - If the list was built from input, check that as much input came in as you expected.
input().split()reads only one line. - For a 2D list, check the row index and the column index in
a[r][c]separately.
Practice problems
See also
- Python KeyError: 'kiwi'
- Python 'int' object is not subscriptable
- Python ValueError: not enough values to unpack
- Syntax: Python Slicing: [start:stop:step] Explained
- Syntax: Python range(): Start, Stop and Step
- Syntax: Python List append(), extend() and insert()
- Learn it step by step: Python Lists and arrays
- All Python error messages
- Online Python interpreter