Re: Dictionary problem
From: Peter Abel (PeterAbel_at_gmx.net)
Date: 11/18/03
- Next message: John Roth: "Re: PEP 321: Date/Time Parsing and Formatting"
- Previous message: John Roth: "Re: Origin of the term "first-class object""
- In reply to: Elena Schulz: "Dictionary problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: John Roth: "Re: PEP 321: Date/Time Parsing and Formatting"
- Previous message: John Roth: "Re: Origin of the term "first-class object""
- In reply to: Elena Schulz: "Dictionary problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|