+ 1

Why line 3 has no effect on list b ?

a = [1,[2,3]] b = a[:] a[0] = 3 a[1][1] = 5 print(b) Output is [1, [2,5]]. I was expecting it to print [3,[2,5]]

6th Mar 2020, 12:42 PM
Peter Parker
Peter Parker - avatar
2 ответов
+ 5
b is a genuine copy of a, so it has its own elements. The list inside b however is a reference to the same list that's in a, so if you manipulate it directly, it changes in both lists (because it's actually one object). As it happens, just yesterday I wrote a little something about how all of that works. Happy if you take a look. 🙂 https://code.sololearn.com/c89ejW97QsTN/?ref=app
6th Mar 2020, 12:49 PM
HonFu
HonFu - avatar
+ 1
HonFu Great tutorial. It is interesting & informative.
6th Mar 2020, 2:08 PM
Peter Parker
Peter Parker - avatar