need explnation for below code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

need explnation for below code

Dear support, kindly help me to understand below code import copy a=[1,[2,3]] b=copy.deepcopy(a) print(b) b=copy.copy(a) print(b) a[0]=3 print(b) a[1][1]=5 print(b) b=copy.copy(a) print(b)

8th Feb 2020, 3:25 PM
manjeet gupta
manjeet gupta - avatar
1 Answer
+ 6
'deepcopy' creates a new deep copy of an object, and 'copy.copy' creates a shallow copy. In the case of a shallow copy the nested list will not be cloned and the result will have the same identical nested list. In the case of a deep copy the nested list will be cloned too, so a new list will be created with new nested lists too. Look at https://docs.python.org/3/library/copy.html
8th Feb 2020, 4:15 PM
SergeiRom