building lists of dictionaries

Jean_Francois Moulin Jean_Francois.Moulin at ph.tum.de
Sun Jul 23 11:02:31 EDT 2006


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
parinfo[0]['fixed'] = 1
parinfo[4]['limited'][0] = 1
parinfo[4]['limits'][0]  = 50.

The first line builds a list of six dictionaries with initialised keys.
I expected that the last three lines would only affect the corresponding keys of the corresponding dictionnary and that I would end up with a fully initialised list where only the 'fixed' key of the first dict would be 1, and the first values of limited and limits for dict number 4 would be 1 and 50. respectively....

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



More information about the Python-list mailing list