Index Out of Range in a Python List

Lists are one of the more unique aspects of the Python programming language. They’re a powerful feature once we’ve fully mastered them. But some issues, such as indexing, can be a little confusing at first. This is especially true for people trying to manipulate a list in the same way they would an array in other languages. Let’s look at how to use indexing with lists and see what a list index out of range Python error means.

# list index out of range python
animals = ["Deer", "Coypu", "Shrew", "Otter"]

In this default value example string we create a list, animals, which contains the name of four different types of mammals for each list element. Then we try to access the last element within the animals list. However, this gives us an indexerror. This is a common mistake among people using Python for the first time, but we can show you the solution to this index error question.

The problem stems from the fact that Python indexes lists from zero rather than one. So when we’re counting through the list we’d actually want to specify [3] for the list index to look at the fourth item in the list. Using [4] produces a range error due to the fact that we’re essentially trying to access an undefined element which doesn’t exist. But how would we know what index value variable the last item in the list is? Take a look at the next python list example to see how we can use the len function to find out by counting n elements.

animals = ["Deer", "Coypu", "Shrew", "Otter"]

In this example we use a function called len in our Python code. We begin by declaring the same list as in the first example. The second line passes that list to the len function as a parameter. The result is then printed to the screen. Finally, the last line uses the len function to determine how long our list is. It then prints out the value stored in the final index position within that list.

You might note one oddity when we’re looking at the assigned index position. Why are we subtracting 1 from the returned len value? The reason comes down to a quick of the len function. Most iterations within Python start from the 0 position. However, len gives us a count based around standard measures. Because of this quirk we’ll need to subtract one from the returned len value in order to make it compatible with the standard list count. With that in mind, we now have the index value for the final list element.

We can also find the last index position within a list’s correct range with a for loop. Let’s take a look at the following code.

ourList = ["Deer", "Coypu", "Shrew", "Otter"]
Listiteration = -1
for n in ourList:
    Listiteration = Listiteration + 1
print ("Final index = " + str(Listiteration))

In this example we use the same list assignment index as before, but work through every element within it using a standard loop script. We begin with a -1 to once again retain compatibility with Python’s method of list indexing. By doing so we can ensure that we don’t end up accessing a non existent item when using the resulting value in our print statement. This list comprehension approach might seem a little overly verbose at first. However, keep in mind that we’ll often find ourselves manually working with data formats which might not have prebuilt functionality within the standard Python programming language. For example, if we’re reading a binary file character by character we might need to fine tune the results a little to avoid an index number error message problem.

In the end, an available index out of range error simply means that we’ve tried to read a value beyond the ending point within a list. Before looking into Python lists, this might seem like a difficult error to avoid. But the main point to remember is that we need to always verify items we’re working with. If we wanted to read a text file then we’d verify that it had opened before trying to read the characters within it. And if we want to access an item in a list we need to ensure that it’s both a list and that we know what its range is.

Index Out of Range in a Python List

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top