Shallow and Deep Copy Python Programming Tutorial

preview_player
Показать описание
In python you can't simply use the = to copy something from one variable/object to another variable/object. To truly copy something you need to make use of the shallow copy or deep copy, this python tutorial shows you why.
Рекомендации по теме
Комментарии
Автор

Very clear and concise. Just enough information and examples to get your point across, but not too much information can clutter and confuse understanding. Thank you.

alexchang
Автор

I've been stuck on this for the past day. Your video has cleared this up perfectly. 7/7 would watch again.

loggitdot
Автор

Great video but there's one thing that wasn't clearly explained and reading through the comments, I think others were a little confused by this.

TLDR
When making modifications on the elements, you have to be check whether the child element (the element you want to modify) is mutable or immutable. Depending on this, the result you get back can be different.

Example below

When you shallow copy arr1 (as shown below), you get arr2. arr1 and arr2 have DIFFERENT id but referencing the same child elements. aka. each child have the same id
arr1 = [1, 2, [3, 4], 5]
arr2 = copy.copy(arr1)
id(arr1) == id(arr2) => FALSE
arr1 == arr2 => TRUE
id(arr1[0]) == id(arr2[0]) => TRUE
id(arr1[1]) == id(arr2[1]) => TRUE
id(arr1[2]) == id(arr2[2]) => TRUE
id(arr1[3]) == id(arr2[3]) => TRUE

Let's modify one of the elements.
arr1[0] = "a"
when you now print arr1 and arr2 you'll get the result as shown below
print(arr1[0]) => ['a', 2, [3, 4], 5]
print(arr2[0]) => [1, 2, [3, 4], 5]
The reason that the first element changed for arr1 but not for arr2 is because the first element `1` is an int which is immutable. Therefore, when you "modify" it from `1` to "a", you essentially create a new string object and now the first element of arr1 is pointing to that new object "a". But the first element of arr2 is still pointing at `1`.

Let's now modify an element in the list
arr1[2][0] = "b"
when you now print arr1 and arr2 you'll get the result as shown below
print(arr1[0]) => ['a', 2, ['b', 4], 5]
print(arr2[0]) => [1, 2, ['b', 4], 5]
This is what really tripped me first! But this is also due to mutability/immutability. Now (looking at the arr1 before modifying), arr1[2] is [3, 4] which is a list, hence mutable. So, modifying that list won't create a new object like above. Therefore, modifying any element inside the list is being shown on both arr1 and arr2.

Long story short, shallow copy does create a copy of a desired collection object. But it is populated with references to the child objects from the original. And depending on whether the element is mutable or not, it can have a very different result

Please let me know if I've made any mistakes. Cheers

liamhan
Автор

This is what I was searching for a long time. clear and on point explanation . Thanks and keep going.

jyotiprakashdas
Автор

Struggeling with Python for weeks. You just solved all my problems

Alphredis
Автор

I wasted a lot of time understanding this, now I finally found this video. Thanks

adarshsasidharan
Автор

Bro you explained so well in a very short time. Saved myself a lot of time. Thanks.

gauravnikalje
Автор

Saw other videos on the same topic that went from 12 minutes to 15 minutes. Amazing how you summarize it in less than 4. Thank you.

taiwoadebisi
Автор

Stole this explanation for tech interviews, thank you :)

moglimogify
Автор

Oh god that was awesome.. You legit explained so much, so clearly in just few minutes.. Hats off man

blaze
Автор

Thanks a lot. Theres no way to make things clear than this.

amadeujoaquim
Автор

Brilliant. Short and concise. Thank you.

kiffbeatz
Автор

the voice is really clean haha so I liked the video 3 s into it. The content is concise.

baoyandong
Автор

There's this one thing to consider. If the elements inside the list are of primitive datatype, their reference won't be copied cus primitive datatypes are copied by value. So, to see shallow copy in action you should use non-primitive datatypes that are list, tuples or dictionaries for the elements of the list.

mohsinalisep
Автор

Very clear explanation. Thanks bro you are lifesaving

handokosupeno
Автор

short and informative. give good examples, easy to understand.

ngzushen
Автор

This video gave the clear understanding of Shallow copy and Deep copy.

Thank you for the video.

Can i know the real time use cases of these functions ?

keshavdk
Автор

Very good and helpful explaination and demonstration my brother 🙏👍.

shashishekhar----
Автор

I did some testing with the module, and I found that after copy.copy if I am making changes in the original list, the changes happen only in that list. The new list has no effect, and vise a versa. However, as shown by you. When we replaced the 2nd element of the first sublist, the changes are happening in both original and new list. Isn't that contradicting?

as
Автор

When would you use a shallow copy over a deep copy? In other words, what is the use of this whole distinction?

JasonRobards