Perhaps understanding the list as such:
new_list = [x[:] for x in old_list]
... although if your matrices are deeper than one layer, list comprehension is probably less elegant than just using deepcopy .
edit - a shallow copy, as indicated, will still contain references to objects within the list. For example...
>>> this = [1, 2] >>> that = [33, 44] >>> stuff = [this, that] >>> other = stuff[:] >>> other [[1, 2], [33, 44]] >>> other[0][0] = False >>> stuff [[False, 2], [33, 44]] #the same problem as before >>> this [False, 2] #original list also changed >>> other = [x[:] for x in stuff] >>> other [[False, 2], [33, 44]] >>> other[0][0] = True >>> other [[True, 2], [33, 44]] >>> stuff [[False, 2], [33, 44]] #copied matrix is different >>> this [False, 2] #original was unchanged by this assignment
user890167
source share