473,394 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

building lists of dictionaries

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
3 1137
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Narendra C. Tulpule | last post by:
Hi, if you know the Python internals, here is a newbie question for you. If I have a list with 100 elements, each element being a long string, is it more efficient to maintain it as a dictionary...
11
by: Amy G | last post by:
I have received such good help on this message board. I wonder if I might not get a little more help from you on this. I am at the point where I have two dictionaries, with information of a...
2
by: Stewart Midwinter | last post by:
I would like to link the contents of three OptionMenu lists. When I select an item from the first list (call it continents), the contents of the 2nd list (call it countries) would update. And in...
1
by: Gabriel B. | last post by:
I just sent an email asking for hints on how to import data into a python program As i said earlier i'm really new to python and besides being confortable with the syntax, i'm not sure if i'm on...
10
by: Philippe C. Martin | last post by:
Hi, I'm looking for an easy algorithm - maybe Python can help: I start with X lists which intial sort is based on list #1. I want to reverse sort list #1 and have all other lists sorted...
9
by: Dave H | last post by:
Hello, I have a query regarding definition lists. Is it good practice semantically to use the dt and dd elements to mark up questions and answers in a frequently asked questions list, or FAQ? ...
9
by: SMB | last post by:
I have two lists of data like the following: LIST1 , ] LIST2 , 'label': 'First Name', 'width': 0L, 'separator': ',', 'height': 0L, 'type': 2L, 'order': 1L}, {'code': 14L, 'name': 'Last...
5
by: Ladislav Andel | last post by:
Hi, I have a list of dictionaries. e.g. how could I make a new list of dictionaries which would look like: , 'service_domain': 'dp0.example.com'}, {'transports': , 'service_domain':...
3
by: ELK | last post by:
I recently started a beginners programming course and am struggling with my latest assignment. I have been given a .txt file of the results of a relay race with the following columns of data: team...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.