list.append not working?

Wildemar Wildenburger wildemar at
Fri Jul 6 00:00:40 CEST 2007

Abhishek Jain wrote:
> 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.


More information about the Python-list mailing list