You don't make a deep copy using list() . (Both list(...) and testList[:] are shallow copies.) You use copy.
I'm using python 3.4.1.
For a single list a=[1,2]
, if I make a copy of it, b = a.copy()
when I change items in b
, it won't change items in a
.
However, when I define a list of lists (actually a matrix) a = [[1,2],[3,4]]
, when I assign b = a.copy()
. What I do to list b
actually affects a
.
I checked their addresses, they are different.
Can anyone tell me why?
ps: What I did is b[0][0] = x
, and the item in a was also changed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With