r/pythonhelp • • Aug 27 '20

Why 2 identical arrays initialized differently give me 2 different results?

Post image
10 Upvotes

6 comments sorted by

View all comments

1

u/nicobautista Aug 27 '20

Im trying to initialize a 2D square array of size n with zeros and then replace only the first row with [1, 2, ..., n]. When I initialize the array using array = [[0]*n]*n, the loop populates all the rows even though I assign it the row index 0. When I manually initialize the array by typing each zero, it does what I want it to do.