list.append not working?

Wildemar Wildenburger wildemar at freakmail.de
Thu Jul 5 17:55:11 EDT 2007


7stud wrote:
> Hardy wrote:
>   
>> 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



More information about the Python-list mailing list