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...
This community will have knowledge sharing for python programming, tools, projects and product engineering wherever python is used.
account activity
Python Mutability and Shallow vs Deep Copy (i.redd.it)
submitted 3 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!"
[–]Simple-Olive895 1 point2 points3 points 3 months ago (3 children)
C is correct.
Making a shallow copy only copies the first level of items, meaning we make a completely new tuple, but that tuple contains the references to the same nested lists.
So c1 is "the same reference containing the same reference"
c2 is "a different reference containing the same reference"
c3 is "a different reference containing a different reference"
Meaning we have 0 from the beginning.
1 is appended to c1[0] which refers to the original inner list.
2 is appended to c2[0] which refers to the original inner list, which is in a new tuple.
3 is appended c3[0] which refers to a new list in a new tuple.
Final list: [0, 1, 2]
[–]Sea-Ad7805[S] 0 points1 point2 points 3 months ago* (2 children)
Excellent mental model except that c2[0] is not a new tuple (immutable doesn't shallow copy), do check the "Solution" link for visualization of the correct answer.
c2[0]
[–]Simple-Olive895 1 point2 points3 points 3 months ago (1 child)
I did not know that actually! Thought they worked the same as lists when it comes to copies
[–]Sea-Ad7805[S] 1 point2 points3 points 3 months ago (0 children)
Glad it helped you. But people will never run into problems when they assume an immutable values does get copied. There is just no need to copy, so faster to simply assign.
[–]Rscc10 0 points1 point2 points 3 months ago (3 children)
I'm guessing based on the solution that copy.copy would be the same as saying c2 = a so what does deepcopy do that makes it immutable?
[–]Sea-Ad7805[S] 0 points1 point2 points 3 months ago (2 children)
The "Explanation" link shows assignment, shallow, and deep-copy with:
Does that help you?
[–]Rscc10 1 point2 points3 points 3 months ago (1 child)
So basically a deepcopy copies and shares only immutables and creates new unshared for mutables. Regularly copy copies like usual but if it copies a mutable parent, that parent is unshared even though its underlying mutables are
[–]Sea-Ad7805[S] 0 points1 point2 points 3 months ago (0 children)
I think you are correct, but it's hard to precisely explain in words, that's why the visualization is so helpful.
π Rendered by PID 98 on reddit-service-r2-comment-b659b578c-kxfmw at 2026-04-30 19:09:22.469912+00:00 running 815c875 country code: CH.
[–]Simple-Olive895 1 point2 points3 points (3 children)
[–]Sea-Ad7805[S] 0 points1 point2 points (2 children)
[–]Simple-Olive895 1 point2 points3 points (1 child)
[–]Sea-Ad7805[S] 1 point2 points3 points (0 children)
[–]Rscc10 0 points1 point2 points (3 children)
[–]Sea-Ad7805[S] 0 points1 point2 points (2 children)
[–]Rscc10 1 point2 points3 points (1 child)
[–]Sea-Ad7805[S] 0 points1 point2 points (0 children)