I am trying to write some code, where in it i need to fill in a matrix and make it the identity matrix and i decided to make this matrix a list of n lists, where each nth list is a row.
I=[]
row=[]
for i in range (0,n):
row.append(0)
for i in range (0,n):
I.append(row)
for k in range (0,3):
I[k][k]=1
I expected this code to put 1's in the matrix's diagonal, where the diagonal are the I[k][k]
elements, k=0,1,2
.
But in the 3rd for loop, where i am replacing the 0's in the diagonal by 1's, in each step, instead of making I[k][k]=1
it makes
I[0][k]=I[1][k]=I[2][k]=1
and I end up with a matrix with every element equal to 1.
My question is not how to make a identity matrix in python, or any matrix, my question is, why is the 3rd for loop doing that, clearly I[k][k]
are diagonal elements, why is it making other non diagonal elements equal to 1?