[newbie] confused about following list assignment

Kenny kccw at well.ox.ac.uk
Fri Feb 27 07:43:55 EST 2004


Hello,

I wanted a list with unique element at postion zero, so I wrote a little
code that I thought would do this (see below).

>>> for i in range(len(a)):
...  x=random.random()
...  print x
...  a[i][0]=str(x*10)
...
0.710829738051
0.896195451411
0.0971075321793
0.882219628625

>>> print a
[['8.82219628625', 'c', 'd'], ['8.82219628625', 'c', 'd'],
['8.82219628625', 'c', 'd'], ['8.82219628625', 'c', 'd']]

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

List a was generated previously as

>>> a=[]
>>> b=['t','c','d']
>>> for i in range(5):
...  a.append(b)

Regards,

Kenny

*******************************************************************************
                     tout casse, tout passe, tout lasse.
*******************************************************************************





More information about the Python-list mailing list