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.
Пікірлер: 98
@alexchang4355 жыл бұрын
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.
@liamhan81453 жыл бұрын
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
@yokeshchowdary15502 жыл бұрын
Clearly explained bro 🥳🥳🥳🥳🥳🥳🔥🔥🔥🔥🔥thanks
@naveennoel94962 жыл бұрын
Thanks!
@bencipherx2 жыл бұрын
Perfectly explained
@saimakhalil5137 Жыл бұрын
jazaKALLAH
@loggitdot4 жыл бұрын
I've been stuck on this for the past day. Your video has cleared this up perfectly. 7/7 would watch again.
@jyotiprakashdas98826 жыл бұрын
This is what I was searching for a long time. clear and on point explanation . Thanks and keep going.
@TejasPatelYT6 жыл бұрын
Thank you, will be starting again soon hopefully (:
@adarshsasidharan2544 жыл бұрын
I wasted a lot of time understanding this, now I finally found this video. Thanks
@Alphredis3 жыл бұрын
Struggeling with Python for weeks. You just solved all my problems
@gauravnikalje8023 жыл бұрын
Bro you explained so well in a very short time. Saved myself a lot of time. Thanks.
@moglimogify3 ай бұрын
Stole this explanation for tech interviews, thank you :)
@taiwoadebisi93152 жыл бұрын
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.
@kiffbeatz Жыл бұрын
Brilliant. Short and concise. Thank you.
@amadeujoaquim2543 Жыл бұрын
Thanks a lot. Theres no way to make things clear than this.
@abdullahassamialnoor27798 ай бұрын
Concise and helpful. Kudos!
@blaze9558 Жыл бұрын
Oh god that was awesome.. You legit explained so much , so clearly in just few minutes.. Hats off man
@subinkv68498 ай бұрын
Good explanation..Thank you..
@cruepprich5 жыл бұрын
Short and sweet! Thanks!
@srenjensen2836 Жыл бұрын
Clear and concise. Great video
@handokosupeno5425 Жыл бұрын
Very clear explanation. Thanks bro you are lifesaving
@mohsinalisep2 жыл бұрын
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.
@arneishprateek6444 Жыл бұрын
More precisely, the elements of the list must be "mutable" objects to see shallow copy in action (see Liam Han's comment below)
@deeek92 Жыл бұрын
Clear and concise explanation!
@baoyandong28484 жыл бұрын
the voice is really clean haha so I liked the video 3 s into it. The content is concise.
@doughntworry5 жыл бұрын
clear and concise. thanks a lot
@ngzushen48333 жыл бұрын
short and informative. give good examples, easy to understand.
@shashishekhar----2 жыл бұрын
Very good and helpful explaination and demonstration my brother 🙏👍.
@EW-mb1ih2 жыл бұрын
Straight to the point, thank you sir
@venkatdevireddy6506 жыл бұрын
very good explanation in just 3 minutes
@kbprojekty5 жыл бұрын
Thanks for the explanation!
@romankalenov90074 жыл бұрын
Brilliantly clear, thanks
@keinermendoza4631 Жыл бұрын
Thanks for the explanation, quite enlightening. 👍
@saurabhchaturvediscientist5 жыл бұрын
Thank you so much! The explanation was clear and concise!
@Ben-ZionMegido Жыл бұрын
Great explanation, thanks
@Vishnuraghutkm4 жыл бұрын
simple good explanation
@nischaysingh2 жыл бұрын
To the point video. Now a days, even for a 2 min video creators consume one minute saying subscribe to my youtube channel 🤣
@sandyhan48294 жыл бұрын
very clear! thank you
@Ali-hr8qu2 жыл бұрын
just wowly explained
@userozancinci4 жыл бұрын
this is so clear thanks!
@as000606 жыл бұрын
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?
@TejasPatelYT6 жыл бұрын
Hi thanks for the comment :) When you say you made changes to that list you must have only changed the 1st dimension elements ie some_list[3] = 'xyz'. So you changed the reference of the 4th element to something else which is why the other list remains the same. There is no contradiction here. The copy.copy only creates a new object but the elements of that object are references to the original elements. So both lists change if you make a change to the element that is being referenced, so making a change to the sublist ie some_list[3][2] = 'xyz' would result in the referenced element to change. Since both new and old list reference the same element you will see the change in both. Hope that clears up your doubts
@sasidharnaidu4507 Жыл бұрын
Copy.copy gives a shallow copy, one level deep. If you want to have a strict new copy, even for the constituent elements of the list, you can go for deep copy.
@polusanisriman8424 Жыл бұрын
Wow.. It's a good video!
@JasonRobards22 жыл бұрын
When would you use a shallow copy over a deep copy? In other words, what is the use of this whole distinction?
@Andres-Estrella Жыл бұрын
so if there are no nested lists a shallow and a deep copy accomplish the same result?
@VascoCC954 жыл бұрын
Very good! Thanks a lot!
@keshavdk30803 жыл бұрын
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 ?
@cy99872 жыл бұрын
Right on point, thank you!
@Enthsz8 ай бұрын
Thanks help a lot
@pranayyanarp41185 жыл бұрын
helped a lot. thanks man.
@TejasPatelYT5 жыл бұрын
Thanks for watching (:
@indiannewyorkbabies68724 жыл бұрын
So in case of Non-compound objects, both shallow and deepcopy does the same job??? And they are different in case of compound objects child object(element change). Is this right ??
@no_life_wth_leafras9492 жыл бұрын
finaly, what's the differencec between x = x and x = copy.copy(x) ?
@SahilThakur265 жыл бұрын
thanks for explaining. i get that its relevant for compound objects(objects containing objects) but interger is also an object in python, so why doesn't it work on them? like if you try this on list of numbers than just nested list, it doesn't work. Could you please comment on this to clear my doubt
@TejasPatelYT5 жыл бұрын
What doesn't work exactly? A shallow copy on a list of number references? ie x = 3 y = 6 nums = [x, y] ?
@satellitesage24875 жыл бұрын
Tejas Patel import copy spam = [1, 2, 3] cheese = copy.copy(spam) cheese[0] = 77 print(spam) print(cheese) ----OUPUT--- [1, 2, 3] [77, 2, 3] We have the same question. If shallow copy means just references to each object, how come this does not apply to noncompund objects, i.e, say just a list of integers? (Demonstrated by the code above).
@TejasPatelYT5 жыл бұрын
Ah yes I probably should of been more clear. Shallow copy would copy over the references of CHILD OBJECTS of a compound object but populate them in a new compound object, non compound object would not have this as there are no child objects so it would just construct a new object for it.
@satellitesage24875 жыл бұрын
Tejas Patel Thank you so much for clearing my confusion!
@ViratHarish5 жыл бұрын
Awesome sir
@pavittarkumarazad32594 жыл бұрын
WOW! Just what I wanted :)
@greyhnd0014 жыл бұрын
I am having trouble using datetime to print to a notepad after running my os telnet python script I want it to append the date and time at the end of the notepad. We have had some instances of people copying our work so the datetime will help stop that.
@ianlawson943 жыл бұрын
Thank you
@janmichaelbesinga38674 жыл бұрын
thanks!
@shirleymoon99342 жыл бұрын
very clear!
@mihaistoian_3 жыл бұрын
Thank you!
@Fine_Mouche3 жыл бұрын
i ended the video, why `new_list = [a,b,c]` work with `copy.copy()` if the reference stay the same ?
@Fine_Mouche3 жыл бұрын
this video answered me : kzbin.info/www/bejne/iIKZhqWViZV6abs
@mikijasar25944 жыл бұрын
Dear Petel, please can you explain to me in the example below why the id of the elements inside the list with shallow copy and deep copy is the same ?? import copy a = [[1,2],[3,4]] z = copy.copy(a) print(a) print(id(a[1][0]),'a 1-0') print(id(z[1][0]),'z 1-0') y = copy.deepcopy(a) print(id(a[1][0]),'a 1-0') print(id(y[1][0]),'y 1-0') Output: [[1, 2], [3, 4]] 267638992 a 1-0 267638992 z 1-0 267638992 a 1-0 267638992 y 1-0 Thanks in advance
@viddeshk80209 ай бұрын
Why a b c is not changed in original list?
@vn75124 жыл бұрын
I'm confused couldn't you just use an already build-in function copy? new_list = old_list.copy() ?
@sonnyhull12132 жыл бұрын
Thanks alot!
@srik863 жыл бұрын
I don’t understand reason behind having shallow copy as a function. It’s neither completely a copy nor completely a reference. Is it there just to confuse people? Why only child objects of a nested list are referenced ? What’s the idea behind this
@priyamvashi21872 ай бұрын
Thank you bhai
@tarams77755 жыл бұрын
Could you please share an example for 1D list?
@sethatkins37315 жыл бұрын
y tho?
@ethanlimrx4 жыл бұрын
Are there any benefits to using a shallow copy? Why not just have deepcopy as standard?
@vn75124 жыл бұрын
my exact thought
@thirumala_reddy5 жыл бұрын
nice explanation thank u
@43SunSon2 жыл бұрын
good video, pika pika
@briansteven63182 жыл бұрын
thank you bruh
@shahzebahmad78664 жыл бұрын
Tq
@acfry10 ай бұрын
thank
@dalinefissi93774 жыл бұрын
very clear gg
@gauravpratapsingh15754 жыл бұрын
I tried this with only one value like: X=5 Y=X Now Y=6 But in the output X remains same it doesn't changes to 6 why?
@shyamgj00914 жыл бұрын
It doesnt work bcos u are initialising Y to X so Y stores the value of X which is 5. Now when u tell Y = 6, Y simply gets updated to 6 and X doesnt change
@ajinzrathod4 жыл бұрын
Instead just use *new_list = old_list[:]* This is slicing Slicing lists does not generate copies of the objects in the list; it just copies the references to them.
@Avinash-st8hd3 жыл бұрын
Wow
@shinkansen19074 жыл бұрын
Dude you talk like a comic book villain, nice video tho thx.
@taihatranduc86132 жыл бұрын
found the bug after hours
@akhilnadhpc6 жыл бұрын
Downvote for not explaining why shallow copy works only in nested list not in single 1D list
@TejasPatelYT6 жыл бұрын
Thanks for your feedback :) I was hoping the shallow copy of the nested list example, showing the whole first element of the list being replaced, would also resemble the same as replacing the first element of a 1D list. So a shallow copy would work with a 1D list, it's when you have multiple nested objects is when a shallow copy does not create a copy of everything.
@4eversuju4 жыл бұрын
@@TejasPatelYT Thank you so much! it really helped me after your explaination here!