I need to assign a predefined dimension to a list of lists. This is because the code within the for
loop is using references to an 'existing' array of such dimension and it would throw an 'index out of range' error if the list is not preloaded before entering the loop.
The following slice of code intended to do such task does not produce the result I would expect.
a, b = 2, 3
matrix = [[[None]*2]*a]*b
for i in range(b):
for j in range(a):
print(i, j)
matrix[i][j] = [i, j]
print(matrix)
I am expecting the elements of the two-dimension list of lists to be a two-elements list. Such as the print statement shows in the following output:
0 0
0 1
1 0
1 1
2 0
2 1
[[[2, 0], [2, 1]], [[2, 0], [2, 1]], [[2, 0], [2, 1]]]
The list of lists should contain the pairs printed.