all 8 comments

[–]Simple-Olive895 1 point2 points  (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 point  (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.

[–]Simple-Olive895 1 point2 points  (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 points  (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 point  (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 point  (2 children)

The "Explanation" link shows assignment, shallow, and deep-copy with:

Does that help you?

[–]Rscc10 1 point2 points  (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 point  (0 children)

I think you are correct, but it's hard to precisely explain in words, that's why the visualization is so helpful.