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
  • 107,083
  • 22
  • 201
  • 376
Eugene
  • 15
  • 5

0 Answers0