What this error means
You tried to use .append() on a tuple, but tuples are immutable—they can't be changed after creation. Only lists have append().
Common causes
- You confused a tuple with a list.
- You expected a list but got a tuple instead.
- You're trying to modify data that should stay fixed.
Example that causes the error
items = (1, 2, 3) # This is a tuple
items.append(4) # AttributeError: tuples can't be modified
How to fix it: use a list instead
If you need to add items, use a list:
items = [1, 2, 3] # This is a list
items.append(4) # OK: lists can be modified
print(items) # [1, 2, 3, 4]
Create a new tuple
If you must use a tuple, create a new one:
items = (1, 2, 3)
new_items = items + (4,) # OK: creates new tuple
print(new_items) # (1, 2, 3, 4)
Convert tuple to list
items = (1, 2, 3)
items_list = list(items) # Convert to list
items_list.append(4) # OK: now it's a list
items = tuple(items_list) # Convert back if needed
Check the type first
data = some_function()
if isinstance(data, list):
data.append("new item")
elif isinstance(data, tuple):
data = data + ("new item",) # Create new tuple