When you write [x]*3
you get, essentially, the list [x, x, x]
. That is, a list with 3 references to x
. When you then change x
all three references are changed.
To fix it, you need to make sure that you create a new list at each position. One way to do it is
[[1]*4for n in range(3)]