DEV Community

Srinivas Ramakrishna for ItsMyCode

Posted on • Originally published at itsmycode.com on

Python IndexError: list index out of range

ItsMyCode |

The IndexError: list index out of range occurs if you access an invalid index in your Python list, Generally it is raised when attempting to retrieve an index from a sequence (e.g., list, tuple, etc), and the index is not found in the given sequence.

Below is a classic example that raises IndexError: list index out of range

test = list(range(53))
test[53]

IndexError: list index out of range
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
c:\Projects\Tryouts\listindexerror.py in 
      1 test = list(range(53))
----> 2 test[53]

IndexError: list index out of range
Enter fullscreen mode Exit fullscreen mode

The error message indicates we are trying to access an index that is not present in the list. In this case, we are trying to print an index of 53 which is not present and in Python, the index number starts from 0, so the first element of the list would be 0, and the last element would be array length-1.

The Problem: IndexError: list index out of range

The common reason which leads to this exception IndexError: list index out of range is that

  • The list is iterated with an index starting from 1 instead of 0.
  • The last item is accessed with the length of the list. It should be accessed as Len(list)-1.
  • When you forget to use*range()*to iterate over a list.

The solution to Indexerror: list index out of range

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.

  • Using len() function
  • Using for loop with ‘in’
  • Using for loop with range()

Solution 1 – Using len() function

If the list is created dynamically, it is difficult to predict the index and access the element. In such a case, always validate the length of the list before accessing the list element.

numbers = [1, 2, 3, 4, 6, 8, 10]
index = 3
if index < len(numbers):
    print(numbers[index])

#Output 4
Enter fullscreen mode Exit fullscreen mode

Solution 2 – Using for loop with ‘in’

Use Membership operators to validate the membership of a value. Membership operators are used to iterating all the elements in a sequence, such as strings, lists, or tuples. The error IndexError: list index out of range will be resolved by the membership operators.

numbers = [1, 2, 3, 4, 6, 8, 10]
for i in numbers:
    print(i)

Enter fullscreen mode Exit fullscreen mode

Solution 3 – Using for loop with range()

The range()function returns a sequence of numbers, starting from 0 by default, increments by 1 (by default), and stops before a specified number. The range function will help in iterating items in the for a loop.

Syntax: range(start, stop, step)

start (optional): an integer that indicates the start of the sequence. The default value is 0 in case left empty.

  • stop: An integer that indicates where the sequence should stop.
  • step (optional): used if you would like to increment by a number greater than 1. The default value is 1
numbers = [1, 2, 3, 4, 6, 8, 10]
for i in range(len(numbers)):
    print(i)

Enter fullscreen mode Exit fullscreen mode

The post Python IndexError: list index out of range appeared first on ItsMyCode.

Top comments (0)