{ }codelper

Error Messages › Python

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])
When you run it
Traceback (most recent call last):
  File "main.py", line 2, in <module>
IndexError: list index out of range

Edit and run it →

Fixed code

Use a[-1] to get the last value without knowing the length. Written as an index, it is a[len(a) - 1].

a = [10, 20, 30]print(a[2])print(a[-1])print(a[len(a) - 1])
Output
30
30
30

Edit and run it →

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])
When you run it
Traceback (most recent call last):
  File "main.py", line 3, in <module>
IndexError: list index out of range

Edit and run it →

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")
Output
none

Edit and run it →

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])
When you run it
3 < 5
4 < 8
Traceback (most recent call last):
  File "main.py", line 3, in <module>
IndexError: list index out of range

Edit and run it →

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)
Output
3 < 5
4 < 8
5 > 4

Edit and run it →

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)
When you run it
Traceback (most recent call last):
  File "main.py", line 3, in <module>
IndexError: list index out of range

Edit and run it →

Fixed code

Do not delete. Build a new list with only the values you want to keep.

a = [1, 2, 3, 4, 5, 6]a = [x for x in a if x % 2 != 0]print(a)
Output
[1, 3, 5]

Edit and run it →

What to check when you see this error

  1. The number after line in the error message is the line where the error happened. Start with the value inside the square brackets [ ] on that line.
  2. Put print(len(a), i) right above that line to see the length and the index for yourself.
  3. If the list was built from input, check that as much input came in as you expected. input().split() reads only one line.
  4. For a 2D list, check the row index and the column index in a[r][c] separately.

Practice problems

See also