Last modified: Dec 06, 2025 By Alexander Williams

Fix Python TypeError: 'range' object item assignment

This error is common for Python beginners. It occurs when you try to change an element inside a range object. This guide explains why and how to fix it.

Understanding the TypeError

The error message is clear. A range object is immutable. You cannot change its items after creation. Trying to do so causes Python to raise a TypeError.

This is similar to the 'tuple' object does not support item assignment error. Both involve immutable sequences.

What is a range object?

The range() function generates a sequence of numbers. It is memory efficient. It yields numbers one by one instead of storing a full list in memory.

Its primary use is in for loops. It is not designed for in-place modification. Treat it as a read-only sequence of integers.

Example of the Error

Here is a simple code snippet that triggers the error. The programmer tries to change the first element of the range.


# This code will cause a TypeError
my_range = range(5)
my_range[0] = 10  # Trying to assign a new value
    

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: 'range' object does not support item assignment
    

How to Fix the Error

The solution is to convert the range to a mutable data type. The most common choice is a list. Lists fully support item assignment.

Use the list() constructor. Wrap your range() call with it. Now you have a list you can modify.

Fix: Convert Range to a List

This is the standard fix. Create a list from the range. Then perform your item assignments on the new list.


# Correct approach: convert to list first
my_list = list(range(5))  # Convert range to list
print("Before:", my_list)
my_list[0] = 10  # This assignment works perfectly
print("After:", my_list)
    

Before: [0, 1, 2, 3, 4]
After: [10, 1, 2, 3, 4]
    

Common Scenarios and Solutions

Let's look at practical situations where this error happens. Each scenario includes a working solution.

Scenario 1: Modifying Loop Indices

You might try to modify values while iterating over a range. This is a direct cause of the error.


# Incorrect: Modifying the range directly
for i in range(5):
    range(5)[i] = i * 2  # Error!

# Correct: Use a list or modify a different variable
values = list(range(5))
for i in range(5):
    values[i] = i * 2  # Works!
print(values)
    

Scenario 2: Using Range for a Mutable Sequence

Sometimes you choose range but later need mutability. Plan ahead and start with a list if you need changes.


# If you need a modifiable number sequence, start with a list.
# Don't use range().
sequence = [0, 10, 20, 30, 40]  # A mutable list
sequence[1] = 15  # Perfectly valid
    

Why is Range Immutable?

Immutability is a design choice for performance and memory efficiency. A range only stores start, stop, and step values.

It calculates values on the fly. This makes it very fast and memory-light for large sequences, like range(1000000).

Allowing item assignment would break this model. It would require storing every number, losing all efficiency benefits.

Related TypeErrors in Python

Python has several immutable types. Attempting item assignment on them causes similar errors. Understanding one helps with others.

For example, a 'set' object is not subscriptable error occurs for similar reasons. Sets are also unordered and not indexable.

Another common error is 'method' object is not subscriptable. It happens when you use square brackets on a method call by mistake.

Best Practices to Avoid the Error

Follow these tips to prevent this TypeError in your code.

Use range only for iteration. Its perfect use is in for loops. Use it to control how many times a loop runs.

Use lists for mutable sequences. If you need to change, add, or remove items, a list is the correct tool from the start.

Know your data types. Be aware of which Python types are mutable (list, dict) and which are immutable (tuple, range, string).

Conclusion

The "TypeError: 'range' object does not support item assignment" error is straightforward. It protects the efficient design of the range object.

The fix is simple: convert the range to a list using list(). Then you can modify the data freely.

Remember this pattern. It applies to other immutable types in Python. Always choose the right data structure for your task to write clean, efficient code.