First post from a Python newbiw

Jeff Schwab jeff at schwabcenter.com
Sun Mar 2 18:23:45 EST 2008


castironpi at gmail.com wrote:
>>>>> 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!

A technically correct solution. :)



More information about the Python-list mailing list