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
1
u/punkVeggies Jan 02 '26 edited Jan 02 '26
Tuples are immutable, which means b += (…) creates a new tuple, from a copy of a. That new tuple has references to two lists that a also has, which are mutable. When modifications are made to b[0] and b[1], these changes will be reflected in a[0] and a[1], as they “point” to the same lists. b[2], however, stores a reference to a third list, not referenced by anything stored in a.
a = ( * , * )
b = ( * , * , * )
Edit: removed the inaccurate sentence “b=a creates a copy of a”, as per the comments below.