Lỗi the index 1 is out of range năm 2024
In order to solve it, you can use calculate the maximal index that can be accessed by the list. You need to make sure that you only access the list element from index 0 until the maximal index.
Show This guide is part of the “Common Python Errors” series. It’s focused entirely on providing quick and easy solutions for Python-related problems. In this tutorial, you’ll learn how all about the Python list index out of range error, including what it is, why it occurs, and how to resolve it. The Throughout this tutorial, you’ll learn why the error occurs and walk through some scenarios where you might encounter it. You’ll also learn how to resolve the error in these scenarios. The Quick Answer: What is the Python IndexError?Let’s take a little bit of time to explore what the Python
We can break down the text a little bit. We can see here that the message tells us that the index is out of range. This means that we are trying to access an index item in a Python list that is out of range, meaning that an item doesn’t have an index position. An item that doesn’t have an index position in a Python list, well, doesn’t exist. In Python, like many other programming languages, a list index begins at position
0 and continues to
1, where n is the length of the list (or the number of items in that list). This causes a fairly common error to occur. Say we are working with a list with 4 items. If we wanted to access the fourth item, you may try to do this by using the index of 4. This, however, would throw the error. This is because the 4th item actually has the index of 3. Let’s take a look at a sample list and try to access an item that doesn’t exist:
We can see here that the index error occurs on the last item we try to access. The simplest solution is to simply not try to access an item that doesn’t exist. But that’s easier said than done. How do we prevent the Need to check if a key exists in a Python dictionary? Check out this tutorial, which teaches you five different ways of seeing if a key exists in a Python dictionary, including how to return a default value. Python IndexError with For LoopYou may encounter the Python
4 function. Let’s take a look at the situation where this error would occur:
The way that we can fix this error from occurring is to simply stop the iteration from occurring before the list runs out of items. The way that we can do this is to change our for loop from going to our length + 1, to the list’s length. When we do this, we stop iterating over the list’s indices before the lengths value. This solves the
6, since our index begins at 0, rather than at 1. Let’s see how we can change the code to run correctly:
Now that you have an understanding of how to resolve the Python Want to learn more about Python for-loops? Check out my in-depth tutorial that takes your from beginner to advanced for-loops user! Want to watch a video instead? Check out my YouTube tutorial here. Python IndexError with While LoopYou may also encounter the Python For example, it may be tempting to run a while loop to iterate over each index position in a list. You may, for example, write a program that looks like this:
The reason that this program fails is that we iterate over the list one too many times. The reason this is true is that we are using a
9 (greater than or equal to sign). Because Python list indices begin at the value 0, their max index is actually equal to the number of items in the list minus 1. We can resolve this by simply changing the operator a less than symbol,
0. This prevents the loop from looping over the index from going out of range.
In the next section, you'll learn a better way to iterate over a Python list to prevent the Want to learn more about Python f-strings? Check out my in-depth tutorial, which includes a step-by-step video to master Python f-strings! How to Fix the Python IndexErrorThere are two simple ways in which you can iterate over a Python list to prevent the Python The first is actually a very plain language way of looping over a list. We don't actually need the list index to iterate over a list. We can simply access its items directly.
This directly prevents Python from going beyond the maximum index. Want to learn how to use the Python
3 function to iterate over two lists? This tutorial teaches you exactly what the
3 function does and shows you some creative ways to use the function. But what if you need to access the list's index?If you need to access the list's index and a list item, then a much safer alternative is to use the Python
5 function. When you pass a list into the
5 function, an enumerate object is returned. This allows you to access both the index and the item for each item in a list. The function implicitly stops at the maximum index, but allows you to get quite a bit of information. Let's take a look at how we can use the
5 function to prevent the Python
We can see here that we the loop stops before the index goes out of range and thereby prevents the Python Check out some other Python tutorials on datagy, including our complete guide to styling Pandas and our comprehensive overview of Pivot Tables in Pandas! ConclusionIn this tutorial, you learned how to understand the Python
5 function. |