Re: Dictionary problem

From: Peter Abel (PeterAbel_at_gmx.net)
Date: 11/18/03


Date: 18 Nov 2003 04:50:53 -0800


"Elena Schulz" <elena.schulz@gmx.net> wrote in message news:<mailman.799.1069086944.702.python-list@python.org>...
> Hi,
>
> I've the following code:
>
> myList = []
> for i in range(3) :
> myDict['id']=i
> myList.append(myDict)
>
> myList becomes: [{'id':2}, {'id':2}, {'id':2}] but I want: [{'id':0},
> {'id':1}, {'id':2}]
>
> thanx for any hint how to achieve that
>
> -- Greetings, Elena
>
> (Please answer to my mail address directly as I am currently not subscribed
> to this list, thanks)

If your desired result is a list whose elements are bound to different
dictionaries, it won't be necessary to bind a dictionary to a variablename
and the bind a copy of it to an new element of a list.
So the shoretest way for me is to append an explicit new dictionary
at the end of list, assumed that your dictionary is as easy as shown
in your example.
So the following works for me so far:

>>> myList=[]
>>> for i in range(3):
... myList.append({'id':i})
...
>>> myList
[{'id': 0}, {'id': 1}, {'id': 2}]
>>>

Regards
Peter



Relevant Pages

  • Re: disk based dictionaries
    ... Best Regards, ... >> I want to store dictionaries on disk. ... >> you are not the intended recipient, please notify the sender at Wipro or Mailadmin@wipro.com immediately ... >> and destroy all copies of this message and any attachments. ...
    (comp.lang.python)
  • Re: NRC kwaliteitskrant? Echt niet!
    ... occured independently to the French and Germans without being a ... At least I found no hint in the two ety ... dictionaries I possess that it be a loan-translation. ... The dictionaries mention so many cognates of in all languages ...
    (sci.lang)
  • Re: combinations listing
    ... Brian Adkins wrote: ... since the anagram and the actual word must be the same length to ... hint above should get you a long way down the road. ... dictionaries be split up? ...
    (comp.lang.ruby)
  • Re: Abbreviation List Tables Design, aka OTLT
    ... The bit about dictionaries is interesting Simon. ... Pick dictionaries is that they are file oriented instead of system oriented. ... Regards ... > provoking to accompany Dawn on her journey to "revalidate" (if thats the ...
    (comp.databases.pick)
  • Re: two-dimensional array
    ... dictionaries. ... I recommend writing to the authors to point it out. ... And no hint of irony! ...
    (comp.lang.c)