+ 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)
1 Resposta
+ 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