Python Tutoring Online
Home

Python List Assignment Index Out of Range Error

The IndexError: list assignment index out of range is another common error encountered when working with Python lists. This error occurs when a program tries to assign a value to an index that does not exist in the list.

Need Help Learning Python?

Understanding common Python errors helps programmers debug their programs quickly and write better code.

If you are learning Python and need help understanding programming errors or debugging your programs, one-to-one Python tutoring can help you progress faster.

Get Python Tutoring Help

Unlike accessing a list element, this error occurs when a program tries to assign a value to an index that is outside the current size of the list.

Understanding List Indexes

Python lists start indexing from 0. If a list contains three elements, the valid indexes are 0, 1, and 2.

Python
numbers = [10,20,30]

print(numbers[0])
print(numbers[1])
print(numbers[2])

Output


10
20
30

Example of Assignment Index Error

Python
numbers = [10,20,30]

numbers[3] = 40

Error Output


IndexError: list assignment index out of range

The list only has indexes 0, 1, and 2. Index 3 does not exist, so Python raises an error.

Cause 1: Assigning Value Beyond List Size

Python
colors = ["red","green","blue"]

colors[5] = "yellow"

Error


IndexError: list assignment index out of range

Correct Way: Use append()

To add elements at the end of a list, use the append() method instead of assigning to a non-existing index.

Python
numbers = [10,20,30]

numbers.append(40)

print(numbers)

Output


[10, 20, 30, 40]

Correct Way: Use insert()

If you want to add an element at a specific position, use the insert() method.

Python
numbers = [10,20,30]

numbers.insert(1,15)

print(numbers)

Output


[10, 15, 20, 30]

Example: Creating a List with Correct Size

Another solution is to create a list with the required number of elements before assigning values.

Python
numbers = [0,0,0,0]

numbers[3] = 40

print(numbers)

Output


[0, 0, 0, 40]

Example with Loop Assignment

This error can also occur inside loops if indexes go beyond the list size.

Python
numbers = [1,2,3]

for i in range(5):
    numbers[i] = i

Error


IndexError: list assignment index out of range

Correct Loop Example

Python
numbers = [1,2,3]

for i in range(len(numbers)):
    numbers[i] = numbers[i] * 2

print(numbers)

Output


[2, 4, 6]

Common Situations Where This Error Occurs

Summary

The IndexError: list assignment index out of range occurs when a program tries to assign a value to an index that does not exist in the list.

This error can be avoided by using methods such as append() and insert(), or by ensuring the list has enough elements before assignment.

Related Python List Tutorials




Stuck Learning Python?

Struggling with Python syntax, assignments or debugging errors? Learning alone can be frustrating.

Get 1-on-1 Python tutoring and learn faster with clear explanations, real examples and live help.

    Get Python Tutoring

PythonTutor.net

Home    Tutorial     Online Live Trainings    Contact Us           © 2026 All rights reserved by www.PythonTutor.net