List of lists surprising behaviour
Boris Borcic
bborcic at gmail.com
Thu Jun 17 09:56:32 EDT 2010
candide wrote:
>
> So what is the right way to initialize to 0 a 2D array ? Is that way
> correct :
>
>
> >>> t=[[0 for _ in range(2)] for _ in range(3)]
That's overkill :) You can skip the inner loop by using a list display, eg
t=[[0,0] for _ in range(3)]
>
> It seems there is no more trouble now :
>
> >>> t
> [[0, 0], [0, 0], [0, 0]]
> >>> t[0][0]=1
> >>> t
> [[1, 0], [0, 0], [0, 0]]
> >>>
>
> Correct ?
More information about the Python-list
mailing list