I am trying to add/replace an i element on the i-th list of a list, however, only the last iteration is saved. It would be great to understand why this happens and what a workaround this would be. I'm interested in an output that looks like this: [[[0], [], [], []], [[1], [], [], []], [[2], [], [], []], [[3], [], [], []]]
, but instead I get this: [[[3], [], [], []], [[3], [], [], []], [[3], [], [], []], [[3], [], [], []]]
matrix = [ [ [] * 4] * 4] * 4
print('pre_matrix=',matrix)
for m in range(len(a) + 1):
matrix[m][0] = [m]
print('m={}, matrix[m]={}'.format(m, matrix[m]))
print('post_matrix=', matrix)
# This is what the response looks like:
# pre_matrix= [[[], [], [], []],
# [[], [], [], []],
# [[], [], [], []],
# [[], [], [], []]]
#
# m=0, matrix[m]=[[0], [], [], []]
# m=1, matrix[m]=[[1], [], [], []]
# m=2, matrix[m]=[[2], [], [], []]
# m=3, matrix[m]=[[3], [], [], []]
#
# post_matrix= [[[3], [], [], []],
# [[3], [], [], []],
# [[3], [], [], []],
# [[3], [], [], []]]