First post from a Python newbiw

castironpi at gmail.com castironpi at gmail.com
Sun Mar 2 17:47:38 EST 2008


> >>> is there a better way of creating d??
>
> >> a = [[0] * 3 for dummy in xrange(3)]
>
> Each element of a refers to a distinct array.
>
> > Why not simply [[0]*3]*3 ?
>
> All three elements of the result refer to the same array.

... whereas you reassign all three elements of [0]* 3.

>>> ((0,)*3,)*3
((0, 0, 0), (0, 0, 0), (0, 0, 0))

You're safe in this one-- changing [0][0] won't change [1][0], 'cuz
you can't!



More information about the Python-list mailing list