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
  • 115,525
  • 42
  • 225
  • 281
lonel
  • 137
  • 5

0 Answers0