I have a 3x3 zero matrix
a = [
[0,0,0],
[0,0,0],
[0,0,0]
]
Suppose I want to add 1
to the first element of the matrix. I use :
a[0][0] += 1
I thought it would add 1 to the a[0][0]
th element and the matrix a
would be :
a = [
[1,0,0],
[0,0,0],
[0,0,0]
]
But , in reality the matrix is now :
a = [
[1,0,0],
[1,0,0],
[1,0,0]
]
Why is 1
added to a[0][1]
and a[0][2]
also ?
Full snippet :
a = [[0] * 3] * 3
a[0][0] += 1
print(a)