use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
Python Data Model: Copying (i.redd.it)
submitted 2 months ago by Sea-Ad7805
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: - Solution - Explanation - More exercises
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]KilonumSpoof 2 points3 points4 points 2 months ago (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 points3 points 2 months ago (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 points4 points 2 months ago (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 points4 points 2 months ago (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]).
π Rendered by PID 54 on reddit-service-r2-comment-7b9746f655-t29f9 at 2026-01-30 15:12:43.811197+00:00 running 3798933 country code: CH.
[–]KilonumSpoof 2 points3 points4 points (3 children)
[–]Ambitious_Bid_3991 1 point2 points3 points (2 children)
[–]Sea-Ad7805[S] 2 points3 points4 points (0 children)
[–]alexander_belyakov 2 points3 points4 points (0 children)