I have the following code in Python 3:
def matrix(n:int) -> list:
matrix = list([[None]*n]*n)
count = 1
for i in range(n):
for j in range(n):
print(i, j, count)
matrix[i][j] = count
count += 1
return matrix
number = 3
print(matrix(number))
Below is the print
result:
0 0 1
0 1 2
0 2 3
1 0 4
1 1 5
1 2 6
2 0 7
2 1 8
2 2 9
[[7, 8, 9], [7, 8, 9], [7, 8, 9]]
As you see, indexes are correct, but it does not return the expected value of [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
. There has to be a fundamental reason for this behavior, and of course, by using other methods, such as append()
, the result is correct.