+ 1

Why is arr[0] * 7 changing all element of arr to [7]

arr =[[]]*3 #[[],[],[]] arr[0].append(7) print(arr[0]) #[7] print(arr) #[[7],[7],[7]] try: print(arr[1][0]) #7 except IndexError: print("0")

28th Apr 2021, 7:11 AM
Gbadegesin Muhammed
Gbadegesin Muhammed - avatar
1 Resposta
+ 3
[[]] * 3 creates a list with 3 identical references of an empty list []. You can change either of the 3 and changes will reflect in all.
28th Apr 2021, 7:28 AM
Ipang