# IndexError List Index Out of Range in Python [Solve]

IndexError: list index out of range error occurs when we tried to access the elements of the list using indexing that is greater than the length of the list. For example, if a list has three elements and you are trying to access an element of the list using index value10, then we will get IndexError: list index out of range error because we are accessing the element using an index that is not in the range of the list. In this short article, we will discuss how we can solve IndexError: list index out of range using various methods. Moreover, we will also cover how to understand Python errors so that in the future we would be able to solve the errors by ourselves.

## Solve IndexError: list index out of range in Python

The error IndexError: list index out of range occurs when we tried to access the element from the list using an index value that is greater than the range of the list. In simple words, we are trying to access the element from the list that is not actually in the range of the list. Let us assume that we have a list and the length of the list is n. That means the last element of the list can be accessed by n-1 because the indexing starts from 0.

The first element of the list can be accessed by index value 0. And the last element of the list can be accessed by index value n-1 where n is equal to the length of the list.

``````# creating a list
list1 = [1,2,3,4,5,6,7]

# index value
list1``````
``````---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
/tmp/ipykernel_373781/2432924903.py in <module>
3
4 # index value
----> 5 list1

IndexError: list index out of range``````

As you can notice we got the error because we have used an index value that is not in the range of the list.

### Solution-1: Using range() method

In general, we can fix the error by making sure that we are not going out of range. We can do this by using the range function and providing the length of the list: The range function starts from 0 ( if not specified) and ends at the specified range.

``````# creating a list
list1 = [1,2,3,4,5,6,7]

# using range function
for i in range(len(list1)):

print(i)``````
``````0
1
2
3
4
5
6``````

Notice that the output shows the index values that can be used to access the elements from the list. If we will use any index value except those shown above, we will get IndexError: list index out of range error.

To access the elements, we can use the following code where we are accessing the elements using the above-given index values

``````# creating a list
list1 = [1,2,3,4,5,6,7]

# using range function
for i in range(len(list1)):

print(list1[i])``````
``````1
2
3
4
5
6
7``````

Notice that we got access to the elements using the index values:

### Solution-2: Using a while loop

In Python, loops are used to iterate through an iterable object. The list is one of the iterable in Python. We can use a while loop to iterate through it and access the elements:

Let us assume that we have a list and we want to access all the elements using a while loop. Here, we will first, indicate the common error that most people made:

``````# creating a list
list1 = ['a', 'b', 'c', 'd', 'e']

# strating index
i  = 0

# using while loop
while i<=len(list1):
print(list1[i])

i+=1``````
``````a
b
c
d
e
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
/tmp/ipykernel_373781/2103807773.py in <module>
7 # using while loop
8 while i<=len(list1):
----> 9     print(list1[i])
10
11     i+=1

IndexError: list index out of range``````

Although we got access to all the elements in the list still getting an error because we are going out of range: To solve the issue we have to subtract 1 from the length of the list. The reason for subtracting 1 is that the indexing starts from 0 which means the first element is accessed with an index value of 0, the second element can be accessed with an index value of 1, and so on. The last element can be accessed with an index value n-1, which is why we need to subtract 1 from the length of the list:

``````# creating a list
list1 = ['a', 'b', 'c', 'd', 'e']

# strating index
i  = 0

# using while loop
while i<=len(list1)-1:
print(list1[i])

i+=1``````
``````a
b
c
d
e``````

Alternatively, we can also change the greater or less than value to just greater than instead of subtracting one from the length:

``````# creating a list
list1 = ['a', 'b', 'c', 'd', 'e']

# strating index
i  = 0

# using while loop
while i<len(list1):
print(list1[i])

i+=1``````
``````a
b
c
d
e``````

As you can see we are no more getting IndexError: list index out of range error.

### Solution-3: Using negative indexing

Python has a cool feature which is we can also use negative indexing to access the elements from the list. The negative indexing starts from -1 which represents the last element, -2 which corresponds to the second last element, and so on.

We can access the last element of the list using negative indexing as shown below:

``````# creating a list
list1 = ['a', 'b', 'c', 'd', 'e']

# negative indexing
list1[-1]``````
``e``

We can also use while loop with negative indexing which will get access to the elements in reverse order:

``````# creating a list
list1 = ['a', 'b', 'c', 'd', 'e']

# strating index
i  = -1

# using while loop
while i>=0-len(list1):
print(list1[i])

i-=1``````
``````e
d
c
b
a``````

Notice that the elements are being accessed in reverse order:

### Solution-4: Using slicing of the list

Another cool feature in Python is slicing. We can access multiple elements from the list using the slicing method. For example, let us say that we want to print only the second and third element from the list:

Here is how you can do that:

``````# creating a list
list1 = ['a', 'b', 'c', 'd', 'e']

# slicing of the list
list1[1:3]``````
``['b', 'c']``

Slicing actually returns a sublist from the main list.

If you want to access the last two elements you can access them using any of the following two methods:

``````# creating a list
list1 = ['a', 'b', 'c', 'd', 'e']

# slicing of the list
list1[3:]
``````
``['d', 'e']``

As you can see, we got the last elements: One feature of slicing is that even if we access elements out of range, we will not get IndexError: list index out of range error. For example, the length of our list is 5 and we want to access elements from 3 up to 30:

``````# creating a list
list1 = ['a', 'b', 'c', 'd', 'e']

# slicing of the list
list1[3:30]``````
``['d', 'e']``

Notice that the code does not return IndexError: list index out of range because in slicing we don’t get this error anymore even if the range is out of the index. You only get the error when you are trying to access individual elements based on indexing.

## Understanding IndexError: list index out of range error

Now let us understand the IndexError: list index out of range error. Similar to any other errors in Python, this also has two main parts. The first parts give information about the category of the error which in this case is the IndexError. The second part gives more specific information about the IndexError by saying which object has the index error problem.

### How the Indexing of a list works in Python?

In Python, the indexing starts from 0. This means the zero indexes correspond to the first element of the list, index 1 corresponds to the second element of the list, and so on:

Python also supports negative indexing. The negative indexing starts from -1 which corresponds to the last element of the list:

## Summary

In this short article, we learned how we can solve IndexError: list index out of range error. We discussed four different methods to solve the error and explained each method. Moreover, we also discussed how indexing works in Python by taking examples.

Scroll to Top