Dear Sir, I have one question about deepcopy if you look at the example below how is it that after deepcoy id of the value of two "different objects" after deepcopy the id of value are the same (line 8 and 9) so here it still seems to be the same element (value) from the list shared by two different objects: 1. import copy 2. old_list = [[1,2],[3,4],[5,6]] 3. deep_list = copy.deepcopy(old_list) 4. print(old_list, 'old_list - deepcopy') 5. print(deep_list, 'deep_list') 6. print(id(old_list), 'id old_list after deepcopy') 7. print(id(deep_list), 'id deep_list') 8. print(id(old_list[1][1]), 'id old_list [1][1]') 9. print(id(deep_list[1][1]), 'id deep_list[1][1]') 10. old_list[1][1] = 'BB' 11. print(old_list, 'old_list - deepcopy') 12. print(deep_list, 'deep_list') 13. print(id(old_list), 'id old_list after deepcopy') 14. print(id(deep_list), 'id deep_list') Output: [[1, 2], [3, 4], [5, 6]] old_list [[1, 2], [3, 4], [5, 6]] deep_list 3419752 id old_list after deepcopy 3778640 id deep_list 267442400 id old_list [1][1] 267442400 id deep_list[1][1] [[1, 2], [3, 'BB'], [5, 6]] old_list - deepcopy [[1, 2], [3, 4], [5, 6]] deep_list 3419752 id old_list after deepcopy 3778640 id deep_list Thank you in advance
Watch more Python Videos Here: th-cam.com/play/PLWPirh4EWFpHYP2n7zUlquPVnGPi5aID7.html&si=HwqNZJVPXnSnSxj3
Shallow copy uses same same memory of old_list to new_list
But Deep copy is creating new memory for new_list.
Right?
Thank you so much, your tutorials are fantastic. You have helped me so much with my studies. I really appreciate it. Best regards
Shallow copy uses same same memory of old_list to new_list
But Deep copy is creating new memory for new_list.
Right?
Nice explaination thank u so much sir
Very nice explanation sir
Dear Sir, I have one question about deepcopy if you look at the example below how is it that after deepcoy id of the value of two "different objects" after deepcopy the id of value are the same (line 8 and 9) so here it still seems to be the same element (value) from the list shared by two different objects:
1. import copy
2. old_list = [[1,2],[3,4],[5,6]]
3. deep_list = copy.deepcopy(old_list)
4. print(old_list, 'old_list - deepcopy')
5. print(deep_list, 'deep_list')
6. print(id(old_list), 'id old_list after deepcopy')
7. print(id(deep_list), 'id deep_list')
8. print(id(old_list[1][1]), 'id old_list [1][1]')
9. print(id(deep_list[1][1]), 'id deep_list[1][1]')
10. old_list[1][1] = 'BB'
11. print(old_list, 'old_list - deepcopy')
12. print(deep_list, 'deep_list')
13. print(id(old_list), 'id old_list after deepcopy')
14. print(id(deep_list), 'id deep_list')
Output:
[[1, 2], [3, 4], [5, 6]] old_list
[[1, 2], [3, 4], [5, 6]] deep_list
3419752 id old_list after deepcopy
3778640 id deep_list
267442400 id old_list [1][1]
267442400 id deep_list[1][1]
[[1, 2], [3, 'BB'], [5, 6]] old_list - deepcopy
[[1, 2], [3, 4], [5, 6]] deep_list
3419752 id old_list after deepcopy
3778640 id deep_list
Thank you in advance
Need to learn to explain
why do people use shallow copy? what are its uses, if its not doing recursively?
in terms of space complexity we use shallow copy as it just copies items of original object as references to copied object
@@ind-ram Shallow copy uses same same memory of old_list to new_list
But Deep copy is creating new memory for new_list.
Right?
@@ALLINONE-fh8pw exactly
I have a doubt, why is [4, 4, 4] not appended to the new_list ??
what are nested objects?
list inside list r called nested list
BEST
Not clear
cha cha har cheez p video bnana zruri nhe h ......