By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,442 Members | 1,485 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,442 IT Pros & Developers. It's quick & easy.

building lists of dictionaries

P: n/a
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
Jul 23 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], 'limits':[0.,0.]}]*6

With this, you are creating a list with 6 references to the same list.
Note that the left operand of '*' is evaluated only once before
"multiplying" it six times.

Regards,
Tito
Jul 23 '06 #2

P: n/a
Jean_Francois Moulin wrote in
news:ma***************************************@pyt hon.org in
comp.lang.python:
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...
This should help:

<url:http://www.python.org/doc/faq/progra...do-i-create-a-
multidimensional-list>

As a TinyUrl: http://tinyurl.com/s8akj

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 23 '06 #3

P: n/a
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.

Jul 23 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.