IndexError: list assignment index out of range in Python

avatar

Borislav Hadzhiev

Last updated: Apr 22, 2022

banner

Photo from Unsplash

IndexError: list assignment index out of range in Python #

The Python "IndexError: list assignment index out of range" occurs when we try to assign a value at an index that doesn't exist in the list. To solve the error, use the append() method to add an item to the end of the list, e.g. my_list.append('b').

indexerror list assignment index out of range

Here is an example of how the error occurs.

main.py
my_list = ['a', 'b', 'c'] # ⛔️ IndexError: list assignment index out of range my_list[3] = 'd'

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

abc
012

Trying to assign a value to any positive index outside the range of 0-2 would cause the IndexError.

If you need to add an item to the end of a list, use the list.append() method instead.

main.py
my_list = ['a', 'b', 'c'] my_list.append('d') my_list.append('e') print(my_list) # 👉️ ['a', 'b', 'c', 'd', 'e']

The list.append() method adds an item to the end of the list.

The method returns None as it mutates the original list.

If you meant to change the value of the last index in the list, use -1.

main.py
my_list = ['a', 'b', 'c'] my_list[-1] = 'z' print(my_list) # 👉️ ['a', 'b', 'z']

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

Alternatively, you can declare a list that contains N elements with None values.

main.py
my_list = [None] * 5 print(my_list) # 👉️ [None, None, None, None, None] my_list[4] = 'hello' print(my_list) # 👉️ [None, None, None, None, 'hello']

The item you specify in the list will be contained N times in the new list the operation returns.

It doesn't have to be None, the value could be 0, an empty string or any other value that suits your use case.

Make sure to wrap the value you want to repeat in a list.

If the list contains a value at the specific index, then you are able to change it.

If you need to get the length of the list, use the len() function.

main.py
my_list = ['a', ' b', 'c'] print(len(my_list)) # 👉️ 3 idx = 3 if len(my_list) > idx: my_list[idx] = 'Z' print(my_list) 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 assign to.

Note that if you try to assign to an empty list at a specific index, you'd always get an IndexError.

main.py
my_list = [] print(my_list) # 👉️ [] print(len(my_list)) # 👉️ 0 # ⛔️ IndexError: list assignment index out of range my_list[0] = 'a'

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

If you need to add multiple items to the end of a list, use the extend() method.

The list.extend method takes an iterable (such as a list) and extends the list by appending all of the items from the iterable.

main.py
my_list = ['a', 'b'] my_list.extend(['c', 'd', 'e']) print(my_list) # 👉️ ['a', 'b', 'c', 'd', 'e']

The list.extend method returns None as it mutates the original list.

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.