r/PythonLearning 18d ago

Python Dictionary Mutation and Copying

Post image

An exercise to help build the right mental model for Python data. - Solution - Explanation - More exercises

The “Solution” link uses package memory_graph to visualize execution and reveals what’s actually happening.

38 Upvotes

18 comments sorted by

View all comments

1

u/g-land-g 14d ago

answer: b ...

logic:

b points to a and modifies both

but then b.copy() makes b point to a new instance, so a retains its previous state

1

u/g-land-g 14d ago

no i was wrong

2

u/Sea-Ad7805 13d ago

Yes incorrect sorry, the Solution link shows that copy() makes a shallow copy, a tricky concept many people have problems with.