There are several ways to resolve indexerror in Python. It is not recommended to access the list or an array in any programming by using a hardcoded index value. It often leads to an exception, instead use one of the below best practices.
Checkout an article list index out of range for more details
- Use len() function before accessing list
Use for loop with ‘in’ to print out list elements
for loop with range() is better way to even access list elements and print them
Checkout an article list index out of range for more details
numbers = [1, 2, 3, 4, 6, 8, 10] index = 3 if index < len(numbers): print(numbers[index])