+ 2
Output of python code
Why does updating the first list update all of them, instead of just the first one? Are they somehow all linked by reference? print(arr := [[]]*3) print(arr[0],arr[1],arr[2]) arr[0].append(7) print(arr[0],arr[1],arr[2])
1 Odpowiedź
+ 1
I believe it’s because it’s using the same list three times.
Try this:
print(arr := [[],[],[]])
print(arr[0],arr[1],arr[2])
arr[0].append(7)
print(arr[0],arr[1],arr[2])