Why is the nested loop assign to all rows the ending loop values?

Viewed 20
from numpy import random

rows, cols = (4, 4)
arr1 = [[0] * cols] * rows

for i in range(rows):
  for j in range(cols):
    arr1[i][j] = i*j
print(arr1)

Output:

[[0, 3, 6, 9], [0, 3, 6, 9], [0, 3, 6, 9], [0, 3, 6, 9]]
0 Answers
Related