Convert list to another form but providing same information

Mark Lawrence breamoreboy at yahoo.co.uk
Mon Mar 21 15:24:39 EDT 2016


On 21/03/2016 18: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...
>

https://docs.python.org/3/faq/programming.html#why-did-changing-list-y-also-change-list-x

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence




More information about the Python-list mailing list