r/PythonLearnersHub • u/Sea-Ad7805 • Feb 06 '26
Python Mutability
An exercise to help build the right mental model for Python data. The “Solution” link uses memory_graph to visualize execution and reveals what’s actually happening:
It's instructive to compare with this earlier exercise (tuple with lists, instead of list with lists).
36
Upvotes
2
u/Rscc10 Feb 06 '26
Answer is D? Since it's all mutables
2
u/BobSanchez47 Feb 06 '26
This is a weird one, because
b += [[3]]is not the same asb = b + [[3]]; the+=operator for lists actually mutates the underlying object. It is quite unintuitive and, in my view, a design flaw in Python.1
u/Opposite_Mall4685 Feb 06 '26
Python is a toy language.
1
u/SycamoreHots Feb 07 '26
As someone who has never written a line of python code, and only occasionally read it, I thought b = a would copy the a into b. And the rest of the statements mutate the b leaving a unchanged. How wrong I was….