+ 1
Confused about Python copy list using a colon?
Can someone please explain why the first case doesn't change the original list and second case does? a=[1,[2,3]] print(a) b=a[:] a[0]=3 a[1][0]=5 print(b) Output: ------- [1, [2, 3]] [1, [5, 3]]
1 Antwort
+ 2
Copy with the colon slice, is actually a shallow copy, it copies only the reference of the nested list. To make sure that all nested structures are copied and become independent of the original, you must use copy.deepcopy() function. Check this for more details.
https://www.google.com/amp/s/www.geeksforgeeks.org/copy-JUMP_LINK__&&__python__&&__JUMP_LINK-deep-copy-shallow-copy/amp/