0
n = 4 
chessboard_lines_initial = [[0]*n]*n
chessboard_lines = copy.deepcopy(chessboard_lines_initial)
print(chessboard_lines)
#Output: 
#[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

chessboard_lines[2][2] = 1
print(chessboard_lines)
#Output: 
#[[0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0]]
#However, I was expecting 
#[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0]]

however, if I directly assign [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] to chessboard_lines, then it works.

Not sure why the copy.deepcopy(chessboard_lines_initial) is not working in this case.

0 Answers0