Lỗi indexerror list index out of range trong python năm 2024

Before we proceed to fixing the error, let's discuss how indexing work in Python lists. You can skip the next section if you already know how indexing works.

How Does Indexing Work in Python Lists?

Each item in a Python list can be assessed using its index number. The first item in a list has an index of zero.

Consider the list below:

languages = ['Python', 'JavaScript', 'Java']
print(languages[1])
# JavaScript

In the example above, we have a list called

languages = ['Python', 'JavaScript', 'Java']
print(languages[3])
# IndexError: list index out of range

0. The list has three items — 'Python', 'JavaScript', and 'Java'.

To access the second item, we used its index:

languages = ['Python', 'JavaScript', 'Java']
print(languages[3])
# IndexError: list index out of range

1. This printed out

languages = ['Python', 'JavaScript', 'Java']
print(languages[3])
# IndexError: list index out of range

2.

Some beginners might misunderstand this. They may assume that since the index is 1, it should be the first item.

To make it easier to understand, here's a breakdown of the items in the list according to their indexes:

Python (item 1) => Index 0 JavaScript (item 2) => Index 1 Java (item 3) => Index 2

As you can see above, the first item has an index of 0 (because Python is "zero-indexed"). To access items in a list, you make use of their indexes.

What Will Happen If You Try to Use an Index That Is Out of Range in a Python List?

If you try to access an item in a list using an index that is out of range, you'll get the IndexError: list index out of range error.

Here's an example:

languages = ['Python', 'JavaScript', 'Java']
print(languages[3])
# IndexError: list index out of range

In the example above, we tried to access a fourth item using its index:

languages = ['Python', 'JavaScript', 'Java']
print(languages[3])
# IndexError: list index out of range

4. We got the IndexError: list index out of range error because the list has no fourth item – it has only three items.

The easy fix is to always use an index that exists in a list when trying to access items in the list.

How to Fix the IndexError: list index out of range Error in Python Loops

Loops work with conditions. So, until a certain condition is met, they'll keep running.

In the example below, we'll try to print all the items in a list using a

languages = ['Python', 'JavaScript', 'Java']
print(languages[3])
# IndexError: list index out of range

7 loop.

languages = ['Python', 'JavaScript', 'Java']
i = 0
while i <= len(languages):
    print(languages[i])
    i += 1
# IndexError: list index out of range

The code above returns the IndexError: list index out of range error. Let's break down the code to understand why this happened.

First, we initialized a variable

languages = ['Python', 'JavaScript', 'Java']
print(languages[3])
# IndexError: list index out of range

9 and gave it a value of 0:

languages = ['Python', 'JavaScript', 'Java']
i = 0
while i <= len(languages):
    print(languages[i])
    i += 1
# IndexError: list index out of range

0.

We then gave a condition for a

languages = ['Python', 'JavaScript', 'Java']
print(languages[3])
# IndexError: list index out of range

7 loop (this is what causes the error):

languages = ['Python', 'JavaScript', 'Java']
i = 0
while i <= len(languages):
    print(languages[i])
    i += 1
# IndexError: list index out of range

2.

From the condition given, we're saying, "this loop should keep running as long as

languages = ['Python', 'JavaScript', 'Java']
print(languages[3])
# IndexError: list index out of range

9 is less than or equal to the length of the

languages = ['Python', 'JavaScript', 'Java']
i = 0
while i <= len(languages):
    print(languages[i])
    i += 1
# IndexError: list index out of range

4 list".

The

languages = ['Python', 'JavaScript', 'Java']
i = 0
while i <= len(languages):
    print(languages[i])
    i += 1
# IndexError: list index out of range

5 function returns the length of the list. In our case, 3 will be returned. So the condition will be this:

languages = ['Python', 'JavaScript', 'Java']
i = 0
while i <= len(languages):
    print(languages[i])
    i += 1
# IndexError: list index out of range

6. The loop will stop when

languages = ['Python', 'JavaScript', 'Java']
print(languages[3])
# IndexError: list index out of range

9 is equal to 3.

Let's pretend to be the Python compiler. Here's what happens as the loop runs.

Here's the list:

languages = ['Python', 'JavaScript', 'Java']
i = 0
while i <= len(languages):
    print(languages[i])
    i += 1
# IndexError: list index out of range

8. It has three indexes — 0, 1, and 2.

When

languages = ['Python', 'JavaScript', 'Java']
print(languages[3])
# IndexError: list index out of range

9 is 0 => Python

When

languages = ['Python', 'JavaScript', 'Java']
print(languages[3])
# IndexError: list index out of range

9 is 1 => JavaScript

When

languages = ['Python', 'JavaScript', 'Java']
print(languages[3])
# IndexError: list index out of range

9 is 2 => Java

When

languages = ['Python', 'JavaScript', 'Java']
print(languages[3])
# IndexError: list index out of range

9 is 3 => Index not found in the list. IndexError: list index out of range error thrown.

So the error is thrown when

languages = ['Python', 'JavaScript', 'Java']
print(languages[3])
# IndexError: list index out of range

9 is equal to 3 because there is no item with an index of 3 in the list.

To fix this problem, we can modify the condition of the loop by removing the equal to sign. This will stop the loop once it gets to the last index.

Here's how:

languages = ['Python', 'JavaScript', 'Java']
i = 0
while i < len(languages):
    print(languages[i])
    i += 1
    # Python
    # JavaScript
    # Java

The condition now looks like this:

languages = ['Python', 'JavaScript', 'Java']
i = 0
while i < len(languages):
    print(languages[i])
    i += 1
    # Python
    # JavaScript
    # Java

5.

The loop will stop at 2 because the condition doesn't allow it to equate to the value returned by the

languages = ['Python', 'JavaScript', 'Java']
i = 0
while i <= len(languages):
    print(languages[i])
    i += 1
# IndexError: list index out of range

5 function.

How to Fix the IndexError: list index out of range Error in When Using the range() Function in Python

By default, the range() function returns a "range" of specified numbers starting from zero.

Here's an example of the range() function in use:

for num in range(5):
  print(num)
    # 0
    # 1
    # 2
    # 3
    # 4

As you can see in the example above,

for num in range(5):
  print(num)
    # 0
    # 1
    # 2
    # 3
    # 4

1 returns 0, 1, 2, 3, 4.

You can use the range() function with a loop to print the items in a list.

The first example will show a code block that throws the IndexError: list index out of range error. After pointing out why the error occurred, we'll fix it.

languages = ['Python', 'JavaScript', 'Java']
for language in range(4):
  print(languages[language])
    # Python
    # JavaScript
    # Java
    # Traceback (most recent call last):
    #   File "", line 5, in 
    # IndexError: list index out of range

The example above prints all the items in the list along with the IndexError: list index out of range error.

We got the error because

for num in range(5):
  print(num)
    # 0
    # 1
    # 2
    # 3
    # 4

5 returns 0, 1, 2, 3. Our list has no index with the value of 3.

To fix this, you can modify the parameter in the range() function. A better solution is to use the length of the list as the range() function's parameter.

That is:

languages = ['Python', 'JavaScript', 'Java']
for language in range(len(languages)):
  print(languages[language])
    # Python
    # JavaScript
    # Java

The code above runs without any error because the

languages = ['Python', 'JavaScript', 'Java']
i = 0
while i <= len(languages):
    print(languages[i])
    i += 1
# IndexError: list index out of range

5 function returns 3. Using that with

for num in range(5):
  print(num)
    # 0
    # 1
    # 2
    # 3
    # 4

9 returns 0, 1, 2 which matches the number of items in a list.

Summary

In this article, we talked about the IndexError: list index out of range error in Python.

This error generally occurs when we try to access an item in a list by using an index that doesn't exist within the list.

We saw some examples that showed how we may get the error when working with loops, the

languages = ['Python', 'JavaScript', 'Java']
i = 0
while i <= len(languages):
    print(languages[i])
    i += 1
# IndexError: list index out of range

5 function, and the range() function.

We also saw how to fix the IndexError: list index out of range error for each case.

Happy coding!



Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started