I have initialised an empty 2d array as follows:
matrixLength = (len(a)+1)*(len(b)+1)
matrix = []
step = []
for i in range(matrixLength):
matrix.append(step)
when I print it, it gives me this:
[[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]
so far so good. But when I want to iterate over the first Len(a) elements of the 2d array, with the following code:
for i in range(0, len(a)+1):
print(i)
matrix[i].append(i)
print (matrix)
instead of getting an array with only the first len(a) elements filled, like this:
[[0], [1], [2], [3], [], [], [], [], [], [], [], [], [], [], [], []]
I get this instead:
[[0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3]]
why?