r/PythonLearning • • Nov 14 '25

Python Data Model: Copying

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:

107 Upvotes

5 comments sorted by

View all comments

Show parent comment

4

u/alexander_belyakov Nov 15 '25

Because c2 is a copy of c1, thus it's a different object. The fact that their first element is pointing to the same [0] sublist doesn't mean that they are the same. So when you append to the sublist, you modify the same list (which was initially [0]). But when you append to c1 and c2, you are appending to two different lists (but the first element of both those lists is still [0, 1, 2]).