Connecting Tech Pros Worldwide Forums | Help | Site Map

Dictionary problem

Elena Schulz
Guest
 
Posts: n/a
#1: Jul 18 '05
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)



Alex Martelli
Guest
 
Posts: n/a
#2: Jul 18 '05

re: Dictionary problem


<posted & mailed>

Elena Schulz wrote:
[color=blue]
> 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}][/color]

So, you don't want to append myDict itself, but, rather, a copy of it.
[color=blue]
> thanx for any hint how to achieve that[/color]

Instead of:

myList.append(myDict)

which appends myDict itself, append a copy, e.g.:

myList.append(myDict.copy())

or

myList.append(dict(myDict))

Python doesn't make copies by default: when you want a copy, you ask
for one explicitly, as in these two examples.
[color=blue]
> (Please answer to my mail address directly as I am currently not
> subscribed to this list, thanks)[/color]

Answering by both posting and mailing as requested.


Alex

Duncan Booth
Guest
 
Posts: n/a
#3: Jul 18 '05

re: Dictionary problem


"Elena Schulz" <elena.schulz@gmx.net> wrote in
news:mailman.799.1069086944.702.python-list@python.org:
[color=blue]
> 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[/color]

Well, firstly I would say that you don't have the code you posted, because
if you did you would be getting:

NameError: name 'myDict' is not defined

Anyway, if you want your list to contain three different dictionaries
instead of three references to the same dictionary, then you have to create
a new dictionary each time. This should do for your example:

myList = []
for i in range(3):
myList.append({'id':i})

Remember that Python never, ever, makes a copy of an object unless you
explicitly tell it to make a copy. Usually all it does is shuffle around
references to the same objects.

If you had a load of other values in your dictionary, and wanted to have
different dictionaries with just the id field different, you could do
something like:

myDict = { 'a': 1, 'b': 2 }
myList = []
for i in range(3):
myDict['id'] = i
myList.append(myDict.copy())


--
Duncan Booth duncan@rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
Peter Abel
Guest
 
Posts: n/a
#4: Jul 18 '05

re: Dictionary problem


"Elena Schulz" <elena.schulz@gmx.net> wrote in message news:<mailman.799.1069086944.702.python-list@python.org>...[color=blue]
> 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)[/color]

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:
[color=blue][color=green][color=darkred]
>>> myList=[]
>>> for i in range(3):[/color][/color][/color]
.... myList.append({'id':i})
....[color=blue][color=green][color=darkred]
>>> myList[/color][/color][/color]
[{'id': 0}, {'id': 1}, {'id': 2}][color=blue][color=green][color=darkred]
>>>[/color][/color][/color]

Regards
Peter
Closed Thread