I want to create a diagonal matrix below's the code in python 2.7
def diag(steps):
'''
steps : a positive integer
'''
matrix = [[0]*steps]*steps # matrix of the order step x step
for i in range(steps + 1):
matrix[i][i] = i + 1 # i'th' element of 'i'th row
return matrix
For eg: if step = 3, I should get [ [1, 0, 0], [0, 2, 0], [0, 0, 3] ]. But I'm getting [ [1, 2, 3], [1, 2, 3], [1, 2, 3] ] Can anyone help me with this bug and please tell what's wrong with my logic?