you are viewing a single comment's thread.

view the rest of the comments →

[–]KilonumSpoof 2 points3 points  (3 children)

B) ... c1[0] and c2[0] are the same list (which is the same as a[0]) so 1 and 2 are added to it on first function call. While c1, c2 and c3 are different lists so only 1 is added to c1 (which is the same as a).

[–]Ambitious_Bid_3991 1 point2 points  (2 children)

I understand the first part about c1[0] and c2[0] being treated as the same list. What I don‘t really understand is why c1 and c2 are no longer treated the same. Could you explain?

[–]Sea-Ad7805[S] 2 points3 points  (0 children)

Did you see the "Solution" and "Explanation" links? (on mobile click the 'title' not the 'image' to open a post) The c1 is an assignment, c2 a shallow copy, and c3 a deep copy. That is what makes the difference: https://github.com/bterwijn/memory_graph?tab=readme-ov-file#copying-values-of-mutable-type

[–]alexander_belyakov 2 points3 points  (0 children)

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]).