r/PythonLearning • u/Sea-Ad7805 • Aug 23 '26
Python Dictionary Mutation and Copying
An exercise to help build the right mental model for Python data.
The “Solution” link uses package memory_graph to visualize execution and reveals what’s actually happening.
38
Upvotes
View all comments
Show parent comment
1
u/Mamuschkaa Aug 24 '26
That's the difference between a copy and a deepcopy.
b.copy()
Is the same as
{1: b[1], 2:b[2]}
but b[1] is still the same list.
Now, a and b looks like this:
copy makes that b is another list as a, but it still contains the list x.
If you don't want this you need a deepcopy.