Dictionary problem

anton muhin antonmuhin.REMOVE.ME.FOR.REAL.MAIL at rambler.ru
Mon Nov 17 14:50:31 EST 2003


Elena Schulz wrote:

> Hi,  Antonmuhin, Alex and Batista,
> 
> as I can use only Python 2.1 dict() is not allowed I guess.
> The .copy() trick works on the simple example I gave. Thanks a lot for your
> hints.
> 
> But my real problem is more complex (nested dicts) like this:
> 
> p_settings = {}
> for item in items :
>    p = myList # a list
>    for i in range(10) :
>       for setting in p[i] :
>           p_settings['anotherString']['aString'] = myString
>    object_dict = {'id':'anotherString', 'settings':p_settings}
>    object_perms_list.append(object_dict)
> 
> here the copy()-trick does not work. How would you solve that?
> 
> -- Thanks for your help, Elena
> 
> 

Isn't clear still---too many unused variables. But you might need 
something like that:

for item in items:
     object_id = {} # new dict created
     object_id['id'] = 'anotherString'
     for i in range(10):
         p_settings = {} # new dict created
         for setting in myList[i]:
              p_settings[setting] = myString
     object_id['settings'] = p_settings
     object_perm_list.append(object_id)

BTW, dicts like {'id': smth, 'setting': smth} looks like objects to me.

And the last thing: it looks like you need to reread doc on references ;)

regards,
anton.





More information about the Python-list mailing list