Duplicating a list through multiplication (*
), shares references for the contents
- multiplication
- mutable
- list
a = [[1]*3]*3
print(a) # [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
a[0][0] = 2
print(a) # [[2, 1, 1], [2, 1, 1], [2, 1, 1]]
Duplicating a list through multiplication (*
), shares references for the contents
a = [[1]*3]*3
print(a) # [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
a[0][0] = 2
print(a) # [[2, 1, 1], [2, 1, 1], [2, 1, 1]]