+ 1

How to create a duplicate list of another list that is not synced?

I saw that a=[3,4,2] , b=a and if a[1]=7 then b[1] also become 7.

5th Jan 2017, 4:04 AM
Tanuj Sharma
Tanuj Sharma - avatar
2 Answers
+ 1
When you do b = a on a mutable object (i.e. list, set, dict, etc.), you create a REFERENCE to the object a. You then just need to do b = a.copy(), b = a[:] or use the copy module
5th Jan 2017, 5:43 PM
Amaras A
Amaras A - avatar
0
a = [1,2,3] b = a c = list(a) a[0] = 'changed' print(a[0]) print(b[0]) print(c[0])
5th Jan 2017, 6:41 AM
richard