“IndexError: list index out of range” in Python

When working with lists in Python, you may come across an “IndexError: list index out of range” error message.
“IndexError: list index out of range” indicates that you are trying to access an index that does not exist in the list.
In this article, we will discuss the causes of this error and how to fix it.
Understanding the Error
An IndexError occurs when you try to access an index that is not available in the list. In Python, the first index of a list is 0, and the last index is len(list)-1. If you try to access an index outside this range, you will get an IndexError.
ERROR Message:
Traceback (most recent call last): File "main.py", line 14, in <module> print(dice[dice_choice]) IndexError: list index out of range
Quick Example to replicate the error:
- Will use a dice example to replicate this issue
- We have a dice choice [1 – 6] stored as a list
- Using a random module, We are choosing a random value between [1,6]
- Considering the random number as an index value to retrieve the dice choice
- This will give a natural imitation of dice rolling
But, It has a bug, Which is intermittent, Code has to be executed multiple times to get the Error
# Reproduce the Bug from random import randint dice = ["❶", "❷", "❸", "❹", "❺", "❻"] dice_choice = randint(1, 6) print(dice[dice_choice])
Hope you got the below error after multiple attempts
Traceback (most recent call last): File "main.py", line 14, in <module> print(dice[dice_choice]) IndexError: list index out of range
Causes of the Error
There are several causes of the “IndexError: list index out of range” error. The most common causes are:
- Accessing an index that does not exist: As mentioned earlier, trying to access an index outside the range of the list will result in an IndexError.
- An empty list: If you try to access an index of an empty list, you will get an IndexError.
- Using the wrong variable: If you have multiple lists in your code and accidentally use the wrong variable to access an index, you will get an IndexError.
In this example, Below piece of code will generate will give you a random number between 1 – 6 like 4,5,3,2,1,4, etc
dice_choice = randint(1, 6)
But, We know the index (offset) starts from “0”,
"❶" -> Offset -> 0 "❷", -> Offset -> 1 "❸", -> Offset -> 2 "❹", -> Offset -> 3 "❺", -> Offset -> 4 "❻" -> Offset -> 5
whenever the “randomint” generates value “6”, We will get “IndexError: list index out of range”. Solution shared at the end of this article 🙂
Fixing the Error
To fix the “IndexError: list index out of range” error, you need to make sure that you are accessing a valid index in the list. Here are some tips to help you fix this error:
- Check the length of the list: Before accessing an index in the list, check the length of the list to make sure that the index you are accessing is within the range of the list.
- Check for an empty list: If you are trying to access an index of a list, make sure that the list is not empty.
- Use the correct variable: If you have multiple lists in your code, make sure that you are using the correct variable to access an index.
Example Code to fix the issue
Let’s consider an example code that will produce an “IndexError: list index out of range” error.
list = [1, 2, 3] print(list[3])
To fix this error, we can check the length of the list before accessing the index:
list = [1, 2, 3] if len(list) > 3: print(list[3]) else: print("Index out of range.")
Output:
Index out of range.
The above code will check the length of the list before accessing the index. If the index is out of range, it will print an error message.
Additionally, in the Solution to the Initial example of rolling a dice, We have to make sure the randint() function input is not going beyond the index value len(list) -1
from random import randint dice_imgs = ["❶", "❷", "❸", "❹", "❺", "❻"] dice_num = randint(0, 5) print(dice_imgs[dice_num])
Output:
❷
Conclusion:
The “IndexError: list index out of range” error occurs when you try to access an index that does not exist in the list. To fix this error, you need to make sure that you are accessing a valid index in the list. Check the length of the list, make sure the list is not empty, and use the correct variable to access the index. By following these tips, you can avoid this error and write better Python code.
Good Luck with your Learning !!