I am writing a function that goes through a 2-dimensional list of zeros (e.g. 5x5) and marks some of them as ones. It follows a pattern and can change directions in between. Here is some snippet of the code (the lower 4 lines are relevant, the rest above is just for context):
def spiralize(size):
spiral = [[0]*size]*size
j = size -1
steps = [j]
while j > 0:
steps.append(j)
steps.append(j)
j -= 2
if size%2 != 0: steps.append(1)
direction = "right"
pointer = [0,0]
for i in steps:
if direction == "right":
for k in range(0,i):
spiral[pointer[0]][pointer[1]] = 1
pointer[1] += 1
I would expect that after that second for-loop, a 5x5 array would look like this:
[[1,1,1,1,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0]])
however, it looks like this:
[[1,1,1,1,0],
[1,1,1,1,0],
[1,1,1,1,0],
[1,1,1,1,0],
[1,1,1,1,0]])
I can't find the reason, why all the other subarrays are affected in the same way. pointer[0] = 0 throughout the whole loop. Is there something about the data structure that I am not aware of?
Thanks you very much in advance.