Dictionary problem

Peter Abel PeterAbel at gmx.net
Tue Nov 18 07:50:53 EST 2003


"Elena Schulz" <elena.schulz at gmx.net> wrote in message news:<mailman.799.1069086944.702.python-list at 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




More information about the Python-list mailing list