1

I wonder

a = [0] * 3
print(a)
b = [a] * 3
print(b)
b[0][0] = 1
print(b)

Why this code's output is

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

Why b[1][0] and b[2][0] are also changed?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Eugene
  • 37
  • 1
  • 6

0 Answers0