Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new example: deep copy list #225

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

KoustavCode
Copy link

No description provided.

Copy link

@OrangeChannel OrangeChannel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't what shallow or deep-copying actually means, copying in any sense returns a different (new) object while assignments use references to the same object. A shallow copy just doesn't recursively copy while a deep copy does. See the following example:

inner = [1,2]
ex_list = [inner, 3, 4]
ex_list_ref = ex_list
ex_list_ref.append(5)

>>> ex_list_ref, ex_list
([[1, 2], 3, 4, 5], [[1, 2], 3, 4, 5])
# both lists were affected

shallow_copy = ex_list[:]
shallow_copy.append(8)

>>> shallow_copy
[[1, 2], 3, 4, 5, 8]
>>> ex_list
[[1, 2], 3, 4, 5]

# however, the `inner` object was not copied, meaning it will change in both
inner.append('t')

>>> shallow_copy
[[1, 2, 't'], 3, 4, 5, 8]
>>> ex_list
[[1, 2, 't'], 3, 4, 5]

# let's look at a deepcopy now
from copy import deepcopy
deep_copy = deepcopy(ex_list)

>>> deep_copy
[[1, 2, 't'], 3, 4, 5]
>>> ex_list
[[1, 2, 't'], 3, 4, 5]

inner.append('f')

>>> deep_copy  # inner should not be affected
[[1, 2, 't'], 3, 4, 5]
>>> ex_list
[[1, 2, 't', 'f'], 3, 4, 5]
# only the original ex_list was affected by updating `inner` as the `inner` object in the deepcopy is copied itself and therefore unique

We could add something like this to clear up confusion about shallow/deep if not already mentioned somewhere but I found the explanation in the docs sufficient.

#### 💡 Explanation:

In Python, we have concept of [deep copy](https://docs.python.org/3/library/copy.html) and [shallow copy](https://docs.python.org/3/library/copy.html).
In the first example, `new_list` is the original list and `new_list_copy` is the shallow copy of the original list. As a result changes made to the copied list has reflected back to the original list.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't quite right, new_list_copy is not a copy of new_list at all. It is simply a reference to it, a simple assignment in Python never creates a copy. You can compare the two lists with is to test.

Comment on lines +1941 to +1943
The second example shows that `new_list_dcopy` is a deep copy of the original list `new_list`, where changes made in the deep copied list doesn't reflect back to the original list.

One can easily deep copy a list without using the *copy.deepcopy(new_list)* just by using `slicing` , where any changes made to the copied list won't reflect back to the original list. Trick!!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, slicing a list using an empty slice will result in a shallow copy of the list, meaning other mutable objects in the new list are not copies of themselves.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants