1
board_1 = [[0]*4]*4
board_2 = [[0,0,0,0], [0,0,0,0], [0,0,0,0], [0,0,0,0]]

both the above methods yield the following list.

[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

but when we replace an element(3,3) in those list we get different result

board_1[3][3] = 1

this will yield

[[0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 0, 1]]

whereas, the below will get the correct output

board_2[3][3] = 1

[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1]]

why we get two different result?

Vignesh Krishnan
  • 743
  • 8
  • 15

0 Answers0