Python Programming Tutorial | copy in Python (Deep Copy and Shallow Copy) | GeeksforGeeks

preview_player
Показать описание

This video is contributed by Parikshit Kumar Pruthi

Please Like, Comment and Share the Video among your friends.

Install our Android App:

Follow us on Facebook:

And Twitter:

Also, Subscribe if you haven't already! :)
Рекомендации по теме
Комментарии
Автор

Hello GeeksforGeeks you are great,
I have a question - data structures which you have presented here for deepcopy I don't think he gives a complete answer because if you look at this example below we see that after deep copy id the values of the two lists are the same (line 12) so that these two objects of these two lists and after deepcopy share the same element. If I am wrong please correct me.
Thanks in advance

import copy
1. lst_deep_1 = [[5, 6]] # creating a list
2. lst_deep_2 = copy.deepcopy(lst_deep_1) # make a deep copy

3.
4. print(id(lst_deep_1), 'lst_deep_1') # print id of lst_deep_1 in fact lst_deep_1 - [[5, 6]]
5. print(id(lst_deep_2), 'lst_deep_2') # print id of lst_deep_2 in fact lst_deep_2 - [[5, 6]]
6. print('id of these two lists is different so we have two different objects')
7. print(id(lst_deep_1[0]), 'lst_deep_1 [0]') # print id of lst_deep_1 nested list in fact lst_deep_1 - [5, 6]
8. print(id(lst_deep_2[0]), 'lst_deep_2 [0]') # print id of lst_deep_2 nested list in fact lst_deep_2 - [5, 6]
9. print('id of lst_deep_1[0] and lst_deep_2[0] are different so we have two different objects inside two object')
10. print(id(lst_deep_1[0][0]), 'lst_deep_1[0][0]' ) # print id of lst_deep_1[0][0] in fact id of lst_deep_1 value 5
11. print(id(lst_deep_2[0][0]), 'lst_deep_2[0][0]' ) # print id of lst_deep_1[0][0] in fact id of lst_deep_2 value 5
12. print('id of lst_deep_1[0][0] and lst_deep_2[0][0] - THEY ARE THE SAME ')
13. print('means these elements are shared by these two objects lst_deep_1 and lst_deep_2 share the same object 5')
14. print(lst_deep_1, 'lst deep 1, id[0][1]', 'id od 6', id(lst_deep_1[0][1]), 'lst_deep_1[0][1]' )
15. print(lst_deep_2, 'lst deep 2, id[0][1]', 'id od 6', id(lst_deep_2[0][1]), 'lst_deep_2[0][1]' )
16. print('also it is the same for value 6 so these two objects lst_deep_1 and lst_deep_2 share the same object 6 ')
17.
18. lst_deep_1[0][1] = 9 # here we change the value 6 to 9
19. print('below we see that the id of [0] [1] in both objects are now different')
20. print(lst_deep_1, 'lst deep 1, id(lst_deep_1[0][1])', 'id od 9', id(lst_deep_1[0][1]))
21. print(lst_deep_2, 'lst deep 2, id(lst_deep_2[0][1])', 'id od 6', id(lst_deep_2[0][1]))
22. print('id of lst_deep_1 and lst_deep_2 are different for both objects so now we have different objects on the index [0][1]')

optupt:

23593648 lst_deep_1
23701944 lst_deep_2
id of these two lists is different so we have two different objects
23701864 lst_deep_1 [0]
23701744 lst_deep_2 [0]
id of lst_deep_1[0] and lst_deep_2[0] are different so we have two different objects inside two object
256628976 lst_deep_1[0][0]
256628976 lst_deep_2[0][0]
id of lst_deep_1[0][0] and lst_deep_2[0][0] - THEY ARE THE SAME
means these elements are shared by these two objects lst_deep_1 and lst_deep_2 share the same object 5
[[5, 6]] lst deep 1, id[0][1] id od 6 256628992 lst_deep_1[0][1]
[[5, 6]] lst deep 2, id[0][1] id od 6 256628992 lst_deep_2[0][1]
also it is the same for value 6 so these two objects lst_deep_1 and lst_deep_2 share the same object 6
below we see that the id of [0] [1] in both objects are now different
[[5, 9]] lst deep 1, id(lst_deep_1[0][1]) id od 9 256629040
[[5, 6]] lst deep 2, id(lst_deep_2[0][1]) id od 6 256628992
id of lst_deep_1 and lst_deep_2 are different for both objects so now we have different objects on the index [0][1]

mikijasar
Автор

Hi,


I have tried the same in both version2 and 3 but not seen any affect on list1 with deepcopy.


Can someone please help me in understanding that why is it not working as shown.


import copy
li1 = [1, 2, [3, 4], 5]
li2 = copy.deepcopy(li1)
print("The original ele before deep copy")
for i in range(0, len(li1)):
print(li1[i], end=' ')
print("\r")
li2[2][0] = 7
print("list1 : ", li1)
print("list2 : ", li2)


Python 3.7.4 (default, Jul 9 2019, 00:06:43)
[GCC 6.3.0 20170516] on linux
The original ele before deep copy
1 2 [3, 4] 5
list1 : [1, 2, [3, 4], 5]
list2 : [1, 2, [7, 4], 5]


Python 2.7.16 (default, Jul 13 2019, 16:01:51)
[GCC 8.3.0] on linux2
The original ele before deep copy
1
2
[3, 4]
5

('list1 : ', [1, 2, [3, 4], 5])
('list2 : ', [1, 2, [7, 4], 5])

nikhilavemuri
Автор

you should mention that it will only work for compound object

ShivamPatel-hlbn
Автор

Hi,
Deepcopy is copy by value & Shallow copy is copy by reference, Please correct me if I am wrong.

akshatzala
Автор

thanks bro..very helpful short and conceptual...

Haiderali-hicj
Автор

Can anyone tell me what's the time complexity of the deep copy operation?

.mhz
Автор

Next video, show how to leave a like in the comment section

MC-etrm
join shbcf.ru