It is not working because when you do cloning or copy it will create shallow copy. So what is shallow copy and deep copy?
Consider, a is list , a = [1,2,3] now when you create new name b= a[:] or b = a.copy(). (Both will give same result. ) . You need to understand what happens at memory level. When we create copy, address reference for list a & b will be changed as in python everything is pass by reference. However, address of the items in the list will remain the same. Please see below snippet.
[Creating list and printing id's of element]
1
Now, we will create a copy and print id of elements inside copied object.
You can see after creating copy, id's of objects inside list has not changed.
However, id's of both the lists will be different as below:
[Id's are different]
3
This is called shallow copy , in which address of objects inside mutable data type will not changed. So, if you pass mutable data type inside of another mutable data type(which is nothing but 2D list) then address will be the same of inside mutable data type. Hence, change done by using name a, will also be reflected in b. Please see below code snippet.
[2-D list overview.]
4
If your requirement is there should not be any change then you can use deepcopy as below code snipped.
[Deep Copy]
5
numpy.b = a[:], you create a new listb, so for examplea.append([5, 6])will not modifyb, as it just changesa. However, the linea[1][0] = 5will changebbecause it changes a list thatbrefers to.a = b.copy(),abecomes a separate place in memory: neither reference nor pointer tob, i.e. it's an independent variable. However, if you doa = b.copy()andbis an array, that does not work. how may there be any logical explanation for that? ifbis an array,a = b.copy()MUST create an independent variable. otherwise it's a bug. p.s. no intention to be rude, pls explain to me if I am wrongawitha = [[1, 2], [3, 4]]. Then you create a copy ofa:b = a.copy. This is a different list, but it contains the same 'sub-lists' this means that changingb, for exampleb.append([5, 6])will not changea, however changing a list inb, for exampleb[0].append(3)will also change the first list ofa.