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. Show 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:
In the example above, we have a list called
0. The list has three items — 'Python', 'JavaScript', and 'Java'. To access the second item, we used its index:
1. This printed out
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 Here's an example:
In the example above, we tried to access a fourth item using its index:
4. We got the 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 LoopsLoops 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
7 loop.
The code above returns the First, we initialized a variable
9 and gave it a value of 0:
0. We then gave a condition for a
7 loop (this is what causes the error):
2. From the condition given, we're saying, "this loop should keep running as long as
9 is less than or equal to the length of the
4 list". The
5 function returns the length of the list. In our case, 3 will be returned. So the condition will be this:
6. The loop will stop when
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:
8. It has three indexes — 0, 1, and 2. When
9 is 0 => Python When
9 is 1 => JavaScript When
9 is 2 => Java When
9 is 3 => Index not found in the list. So the error is thrown when
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:
The condition now looks like this:
5. The loop will stop at 2 because the condition doesn't allow it to equate to the value returned by the
5 function. How to Fix the IndexError: list index out of range Error in When Using the range() Function in PythonBy default, the Here's an example of the
As you can see in the example above,
1 returns 0, 1, 2, 3, 4. You can use the The first example will show a code block that throws the
The example above prints all the items in the list along with the We got the error because
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 That is:
The code above runs without any error because the
5 function returns 3. Using that with
9 returns 0, 1, 2 which matches the number of items in a list. SummaryIn this article, we talked about the 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
5 function, and the We also saw how to fix the 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 |