Home βΊ Problems βΊ Trying to modify a tuple
Trying to modify a tuple
π Fix the bug Topic: tuples Difficulty: easy Free
What this problem practises
Tuples are immutable containers that may themselves hold mutable objects, and it is the comma β not the parentheses β that builds one. Any unpacking where the counts disagree raises ValueError.
This is a fix-the-bug problem: a short program is given in its broken state, and the task is to repair the code so it produces the required output. The interactive editor runs your fixed version in the browser and checks it for you β no setup, no local interpreter.
Difficulty: easy β the fix or the trick is normally a single line, and a careful read should be enough.
The challenge
This should update a coordinate, but crashes. Fix it.
The code runs in PyDebugβs in-browser compiler. Open the problem, run the snippet, watch the exact error or output, and keep iterating until it is right. Signed-in solves count toward XP and your daily streak.
The error it raises
Run the broken snippet and Python stops with this exact error: TypeError: 'tuple' object does not support item assignment. Match it to the Python errors guide, then repair the line that produced it β knowing the message is half the fix.
The snippet
The interactive editor pre-fills exactly this code, broken and ready to repair:
point = (3, 4)
point[0] = 10
print(point)Understand the rule
TypeError: 'tuple' object does not support item assignment enforces the whole point of tuples: once built, their slots never change. point[0] = 5 fails no matter what the tuple holds. The repair is a design decision β either the data was meant to change (use a list) or a modified copy should be built (construct a new tuple).
How to trace it
- Find the assignment into an index: name[i] = value on something created with parentheses or commas.
- Decide mutability: does this data logically change over time? If yes, create it as a list instead.
- If the tuple should stay a tuple, build a replacement: new = old[:i] + (value,) + old[i+1:].
- Remember the one-element tuple needs a comma β (value,) not (value).
Common wrong fixes
- Converting to a list, mutating, and converting back everywhere, which negates the safety the tuple was providing.
- Believing a tuple freezes its contents deeply: a list inside a tuple can still be mutated through it.
- Blaming the data ('it should have been a list') when the real fix is producing a new tuple functionally.
Rule to remember
Tuples are immutable by design: item assignment always raises TypeError. Choose a list when the data changes, or build a new tuple when a modified copy is what you mean.
More tuples problems
Keep going
- Python debugging guide β a systematic method for finding bugs like this one fast.
- Browse every tuples problem in this topic.
- Common Python errors and what they actually mean, plus the Python glossary for any term used on this page.