On Tuesday, March 22, 2016 at 12:01:10 AM UTC+5:30, Maurice wrote:
> Just figured why:
> 
> If I type this on the kernel:
> 
> weirdList = [[0]*3]*5
> 
> weirdList
> Out[257]: [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
> 
> weirdList[0][0] = 1
> 
> weirdList
> Out[259]: [[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0]]
> 
> All first elements of the sublists also changes. I dunno why...

When we write [[0] * 3] * 5, the [0] * 3 list is repeated 5 times
its something like
list,list,list,list,list

if we change the 1st element of list, it gets reflected on all the 5 copies
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to