I am having two strange problems with python.
First of all, when I assign a value to a nested list like foo[0][0] = 1
, foo
is changed to [[1, 0, 0], [1, 0, 0], [1, 0, 0]]
.
Secondly, even when I use .copy()
, it assigns the same thing to the original value.
>>> foo = [[0]*3]*3
>>> bar = foo.copy()
>>> bar[0][0] = 1
>>> bar
[[1, 0, 0], [1, 0, 0], [1, 0, 0]]
>>> foo
[[1, 0, 0], [1, 0, 0], [1, 0, 0]]
I need bar
to be changed to [[1, 0, 0], [0, 0, 0], [0, 0, 0]]
instead, and for foo
to stay the same.
How can I do this?