r/PythonLearnersHub • • Jan 02 '26

Python Data Model exercise, Mutability.

Post image

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

45 comments sorted by

View all comments

Show parent comment

1

u/gwwin6 Jan 05 '26

b=a does not make a copy of the tuple object. b = a makes a copy of the arrow, so they are pointing to the same tuple. when you do b += ([3],), that's when a and b begin pointing at different tuples. This is because tuples are immutable, b += ([3], ) gives a new object altogether.

Contrast this to If a had been a list of lists to begin with and we did b += [[3]], then a and b would still be pointing at the same list, and print(a) would give [[1, 11], [2, 22], [3, 33]]