list.append not working?

H

Hardy

I experience a problem with append(). This is a part of my code:

for entity in temp:
md['module']= entity.addr.get('module')
md['id']=entity.addr.get('id')
md['type']=entity.addr.get('type')
#print md
mbusentities.append(md)
#print mbusentities

I want something like: [{'module': 'home', 'id': 123, 'type': 'core'},
{'module': 'work', 'id': 456, 'type': 'core'}]
md is always correct, BUT:mbusentities is wrong. Length of
mbusentities is same of temp, so it appended everything. BUT:
mbusentities only shows the values of the last append: [{'module':
'work', 'id': 456, 'type': 'core'}, {'module': 'work', 'id': 456,
'type': 'core'}]

What's wrong?
 
I

infidel

I experience a problem with append(). This is a part of my code:

for entity in temp:
md['module']= entity.addr.get('module')
md['id']=entity.addr.get('id')
md['type']=entity.addr.get('type')
#print md
mbusentities.append(md)
#print mbusentities

I want something like: [{'module': 'home', 'id': 123, 'type': 'core'},
{'module': 'work', 'id': 456, 'type': 'core'}]
md is always correct, BUT:mbusentities is wrong. Length of
mbusentities is same of temp, so it appended everything. BUT:
mbusentities only shows the values of the last append: [{'module':
'work', 'id': 456, 'type': 'core'}, {'module': 'work', 'id': 456,
'type': 'core'}]

What's wrong?

You're reusing the same "md" dictionary over and over, appending the
same object to the list each time. So what you have is a list of
references to the same dictionary. You need to set md = {} first
thing each iteration.
 
7

7stud

Hardy said:
I experience a problem with append(). This is a part of my code:

for entity in temp:
md['module']= entity.addr.get('module')
md['id']=entity.addr.get('id')
md['type']=entity.addr.get('type')
#print md
mbusentities.append(md)
#print mbusentities

I want something like: [{'module': 'home', 'id': 123, 'type': 'core'},
{'module': 'work', 'id': 456, 'type': 'core'}]
md is always correct, BUT:mbusentities is wrong. Length of
mbusentities is same of temp, so it appended everything. BUT:
mbusentities only shows the values of the last append: [{'module':
'work', 'id': 456, 'type': 'core'}, {'module': 'work', 'id': 456,
'type': 'core'}]

What's wrong?

Inside your loop, you assign md["module"], md["id"], and md["type"]
the same values over and over again.
 
H

Hardy

I experience a problem with append(). This is a part of my code:
for entity in temp:
md['module']= entity.addr.get('module')
md['id']=entity.addr.get('id')
md['type']=entity.addr.get('type')
#print md
mbusentities.append(md)
#print mbusentities
I want something like: [{'module': 'home', 'id': 123, 'type': 'core'},
{'module': 'work', 'id': 456, 'type': 'core'}]
md is always correct, BUT:mbusentities is wrong. Length of
mbusentities is same of temp, so it appended everything. BUT:
mbusentities only shows the values of the last append: [{'module':
'work', 'id': 456, 'type': 'core'}, {'module': 'work', 'id': 456,
'type': 'core'}]
What's wrong?

You're reusing the same "md" dictionary over and over, appending the
same object to the list each time. So what you have is a list of
references to the same dictionary. You need to set md = {} first
thing each iteration.

Thanks, that was my mistake, should take a break, getting code-blind :D
 
W

Wildemar Wildenburger

7stud said:
Hardy said:
I experience a problem with append(). This is a part of my code:

for entity in temp:
md['module']= entity.addr.get('module')
md['id']=entity.addr.get('id')
md['type']=entity.addr.get('type')
#print md
mbusentities.append(md)
#print mbusentities

I want something like: [{'module': 'home', 'id': 123, 'type': 'core'},
{'module': 'work', 'id': 456, 'type': 'core'}]
md is always correct, BUT:mbusentities is wrong. Length of
mbusentities is same of temp, so it appended everything. BUT:
mbusentities only shows the values of the last append: [{'module':
'work', 'id': 456, 'type': 'core'}, {'module': 'work', 'id': 456,
'type': 'core'}]

What's wrong?

Inside your loop, you assign md["module"], md["id"], and md["type"]
the same values over and over again.
I may seem like a prick, but that is not the correct answer (infidel and
Abhishek gave the correct explanation). I just thought I'd point this
out to avoid confusion.

/W
 
W

Wildemar Wildenburger

Abhishek said:
with every iteration your previous values are overwritten ('md' is a
dictionary) so thats why your are observing this ouput..

check if the following patch solves your problem

for entity in temp:
md['module']= entity.addr.get('module')
md['id']=entity.addr.get('id')
md['type']=entity.addr.get('type')
#print md
mbusentities.append(md)
md = {}
#print mbusentities


Regards
Abhi
This will work, but may I suggest putting the md = {} line at the
*beginning* of the loop?
I find seeing it at the end HIGHLY confusing. Declaring it in the
beginning makes sense, because you declare/initialize, then use it. But
using and *then* initializing it for the next iteration is kind of
quirky, because it breaks the logical encapsulation I would like to see
in *one* loop iteration.

/W
 
R

Rex Turnbull

Wildemar said:
Abhishek said:
with every iteration your previous values are overwritten ('md' is a
dictionary) so thats why your are observing this ouput..

check if the following patch solves your problem

for entity in temp:
md['module']= entity.addr.get('module')
md['id']=entity.addr.get('id')
md['type']=entity.addr.get('type')
#print md
mbusentities.append(md)
md = {}
#print mbusentities


Regards
Abhi
This will work, but may I suggest putting the md = {} line at the
*beginning* of the loop?
I find seeing it at the end HIGHLY confusing. Declaring it in the
beginning makes sense, because you declare/initialize, then use it. But
using and *then* initializing it for the next iteration is kind of
quirky, because it breaks the logical encapsulation I would like to see
in *one* loop iteration.

/W

Hear, hear! to the declaration at the beginning. Just went through a
long bug search due to a similar behaviour. Terrible. I try to view
the body of a loop as if it were a separate function/method. If it
makes sense from that point of view, it will make sense in 3 months. I
mean, why would you want ot initialize something when you're done with it?

Cheers,
Rex
 
B

Bruno Desthuilliers

Hardy a écrit :
I experience a problem with append(). This is a part of my code:
for entity in temp:
md['module']= entity.addr.get('module')
md['id']=entity.addr.get('id')
md['type']=entity.addr.get('type')
#print md
mbusentities.append(md)
#print mbusentities
I want something like: [{'module': 'home', 'id': 123, 'type': 'core'},
{'module': 'work', 'id': 456, 'type': 'core'}]
md is always correct, BUT:mbusentities is wrong. Length of
mbusentities is same of temp, so it appended everything. BUT:
mbusentities only shows the values of the last append: [{'module':
'work', 'id': 456, 'type': 'core'}, {'module': 'work', 'id': 456,
'type': 'core'}]
What's wrong?
You're reusing the same "md" dictionary over and over, appending the
same object to the list each time. So what you have is a list of
references to the same dictionary. You need to set md = {} first
thing each iteration.

Thanks, that was my mistake, should take a break, getting code-blind :D

append = mbusentities.append
for entity in temp:
get = entity.addr.get
append(dict((k, get(k)) for k in ('module', 'id', 'type')))
 

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,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top