building lists of dictionaries
Juho Schultz
juho.schultz at pp.inet.fi
Sun Jul 23 12:01:08 EDT 2006
Jean_Francois Moulin wrote:
> Hi all,
>
> I tried this piece of code (FWIW, it was taken as is from a help section of mpfit, a mathematical routine for least square fitting):
>
> parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], 'limits':[0.,0.]}]*6
> The first line builds a list of six dictionaries with initialised keys.
> This is not so!
> I end up with all dictionaries being identical and having their 'fixed' key set to 1, and limited[0]==1 and limits[0]==50.
>
> I do not understand this behaviour...
>
> Thanks for helping a newbie.
>
> JF
xvec = [{'value':0}]*6
xids = [id(x) for x in xvec]
print xids
Should print a list of six integers, all equal.
So all elements in your list are the same.
Another way to construct the desired list:
yvec = [{'value':0} for i in range(6)]
yids = [id(y) for y in yvec]
print yids
The elements in this list should be all different.
More information about the Python-list
mailing list