Dictionary problem

E

Elena Schulz

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)
 
A

Alex Martelli

<posted & mailed>

Elena said:
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}]

So, you don't want to append myDict itself, but, rather, a copy of it.
thanx for any hint how to achieve that

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.
(Please answer to my mail address directly as I am currently not
subscribed to this list, thanks)

Answering by both posting and mailing as requested.


Alex
 
D

Duncan Booth

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

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())
 
P

Peter Abel

Elena Schulz said:
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.append({'id':i})
....
myList [{'id': 0}, {'id': 1}, {'id': 2}]

Regards
Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,466
Latest member
DrusillaYa

Latest Threads

Top