+ 1
Why does this change two zeroes to 5 instead of just the first one?
a = [[0]*2]*2 a[0][0] = 5 print(a) #[5,0][5,0]
2 ответов
+ 5
print(n*list) prints n representations of the same list.
Likewise, a = [[0]*2]*2 creates a nested list with two inner lists that are the same. If you change one, you change the other one as well.
Use a = [[0 for i in range(2)] for j in range(2)] to create a nested list with two independent inner lists.
+ 1
Thanks again! Anna