Solve - IndexError: list index out of range in Python


Borislav Hadzhiev

Last updated: Apr 22, 2022


Check out my new book

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

The Python "IndexError: list index out of range" occurs when we try to access an index that doesn't exist in a list. Indexes are zero-based in Python, so the index of the first item in the list is 0, and the index of the last is -1 or len(my_list) - 1.

indexerror list assignment index out of range

Here is an example of how the error occurs.
my_list = ['a', 'b', 'c'] # ⛔️ IndexError: list index out of range print(my_list[3])

The list has a length of 3. Since indexes in Python are zero-based, the first item in the list has an index of 0, and the last an index of 2.

If we try to access any positive index outside the range of 0-2, we would get an IndexError.

If you need to get the last item in a list, use -1.
my_list = ['a', 'b', 'c'] print(my_list[-1]) # 👉️ c print(my_list[-2]) # 👉️ b

When the index starts with a minus, we start counting backwards from the end of the list.

If you need to get the length of the list, use the len() function.
my_list = ['a', 'b', 'c'] print(len(my_list)) # 👉️ 3 idx = 3 if len(my_list) > idx: print(my_list[idx]) else: # 👇️ this runs print(f'index {idx} is out of range')

The len() function returns the length (the number of items) of an object.

The argument the function takes may be a sequence (a string, tuple, list, range or bytes) or a collection (a dictionary, set, or frozen set).

If a list has a length of 3, then its last index is 2 (because indexes are zero-based).

This means that you can check if the list's length is greater than the index you are trying to access.

If you are trying to iterate over a list using the range() function, use the length of the list.
my_list = ['a', 'b', 'c'] for i in range(len(my_list)): print(i, my_list[i]) # 👉️ 0 a, 1 b, 2 c

However, a much better solution is to use the enumerate() function to iterate over a list with the index.
my_list = ['apple', 'banana', 'melon'] for index, item in enumerate(my_list): print(index, item) # 👉️ 0 apple, 1 banana, 2 melon

The enumerate function takes an iterable and returns an enumerate object containing tuples where the first element is the index, and the second, the item.

An alternative approach to handle the "IndexError: list index out of range" exception is to use a try/except block.
my_list = ['a', 'b', 'c'] try: result = my_list[100] except IndexError: print('index out of range') # handle error here

We tried accessing the list item at index 100 which raised an IndexError exception.

You can handle the error or use the pass keyword in the except block.

Note that if you try to access an empty list at a specific index, you'd always get an IndexError.
my_list = [] print(my_list) print(len(my_list)) # ⛔️ IndexError: list index out of range print(my_list[0])

You should print the list you are trying to access and its length to make sure the variable stores what you expect.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.