0

If I create a two dimensional matrix as follows:

n = 5
m = 5
l = [[0] * n] * m
l[0][0] = 1
print(l)

will output:

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

Why? why are all the sub-lists first elements being set to 1??

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
lonel
  • 137
  • 6

0 Answers0