[newbie] confused about following list assignment

KAP kpacera at highstream.net
Sun Feb 29 00:16:55 EST 2004


On Fri, 27 Feb 2004 12:43:55 +0000, Kenny <kccw at well.ox.ac.uk> wrote:

>
> Can someone explain why all the position zeros are being assigned the 
> same value of x?

Try >>> for x in range(len(a)): print id(a[x][0])

to see that all reference the same object. The last assignment you made 
with "a[i][0]=str(x*10)" is the new object that each of the above will 
reference.

You need to create some copies first. Your code will work the way you want 
by changing:

...  a.append(b)

to this

...  a.append(b[:])

Ken



More information about the Python-list mailing list