Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python copy a list of lists [duplicate]

Tags:

People also ask

Is list copy deep copy?

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.