TypeError
TypeError: 'tuple' object does not support item assignment
Traceback
Traceback (most recent call last):
File "main.py", line 2, in <module>
point[0] = 10
TypeError: 'tuple' object does not support item assignmentWhat causes this error
An attempt was made to modify an element of a tuple using index assignment. Tuples are immutable and do not support item modification.
How to fix it
Use a list instead of a tuple if you need mutability. Convert tuple to list, modify, and convert back: `lst = list(t); lst[0] = val; t = tuple(lst)`. Use named tuples or dataclasses for structured data.
Code that causes this error
point = (1, 2, 3) point[0] = 10
Fixed code
point = (1, 2, 3) point = (10,) + point[1:] # or use a list: point = [1, 2, 3] point[0] = 10
About TypeError
This TypeError occurs when you try to modify an element of a tuple using index assignment. Like strings, tuples are immutable in Python — they cannot be modified after creation. Tuples are designed for fixed collections of data, often used for function return values, dictionary keys, and named data records.
If you need mutability, use a list instead. To modify a tuple, convert it to a list, make changes, and convert back. Note that while a tuple itself is immutable, mutable objects inside a tuple (like lists) can still be modified — `t = ([1, 2],)` allows `t[0].append(3)` but not `t[0] = [4, 5]`.
This distinction between the tuple's own immutability and the mutability of its contents is important to understand.
Common scenarios
Performing operations between incompatible types like strings and integers
Calling methods that return None and chaining operations on the result
Passing the wrong number or type of arguments to a function
Using bracket notation on objects that do not support indexing