+ 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])

1st Sep 2021, 4:36 AM
Edward Finkelstein
Edward Finkelstein - avatar
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])
6th Oct 2021, 2:05 PM
Yubo Wang