I'm having some problems with the following code (Python):
x = [0,0]
y = [[1,1],[2,2],[3,3]]
z = y
# z = [[1,1],[2,2],[3,3]]
print(y)
for i in range(0,len(z)):
if i == 0:
z[i] = x
else:
z[i] = y[i-1]
print(y)
y = z
print(y)
for i in range(0,len(z)):
if i == 0:
z[i] = x
else:
z[i] = y[i-1]
print(y)
What I'd expect as the output is
[[1, 1], [2, 2], [3, 3]]
[[1, 1], [2, 2], [3, 3]]
[[0, 0], [1, 1], [2, 2]]
[[0, 0], [1, 1], [2, 2]]
but what I get is
[[1, 1], [2, 2], [3, 3]]
[[0, 0], [0, 0], [0, 0]]
[[0, 0], [0, 0], [0, 0]]
[[0, 0], [0, 0], [0, 0]]
and when I instead use the line I commented out for z, I get
[[1, 1], [2, 2], [3, 3]]
[[1, 1], [2, 2], [3, 3]]
[[0, 0], [1, 1], [2, 2]]
[[0, 0], [0, 0], [0, 0]]
Why aren't lines 1&2 and 3&4 of the output always the same? As far as I can tell, I'm not doing anything to change y (other than y=z) and I don't understand why using z=y or z = [...] at the beginning makes a difference.