0

I have this code:
A 3X3 matrix of zeros
When i set '5' to the 0,0 element of the matrix,
the entire columns is set to '5'
i tried to debug it but i can't

p = [[0]*3]*3
p[0][0] = 5
for i in p:
    print(i)

#prints:
#[5, 0, 0]
#[5, 0, 0]
#[5, 0, 0]

1 Answers1

2

It's because the first element in each list within p is actually referencing the same thing, so you've effectively created aliases. Check out this answer for creating "empty" lists of lists.

ZaxR
  • 4,896
  • 4
  • 23
  • 42