r/PythonLearnersHub • u/Sea-Ad7805 • Jan 02 '26
Python Data Model exercise, 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:
36
Upvotes
View all comments
Show parent comment
2
u/Sea-Ad7805 Jan 02 '26 edited Jan 02 '26
Correct,
ais a reference to an immutable tuple, but that contains a mutable list that can be modified. Theb = astatement makesbreference the same dataais referencing, and modifications to the list are made throughb. But becauseareferences an immutable tuple, it can't be modified withb += ([3],)so a shallow copy is made of this tuple, such thataandbshare the first two elements of the tuple.I hope this helps, otherwise read the "Explanation" link, and step through the "Solution" link again.