Nested dictionary cleanup

Colin Brown cbrown at metservice.com
Mon Nov 3 16:35:50 EST 2003


OOPS!

Coding slip I'm afraid, sorry. I was thinking that the
code was failing because keys() were being evaluated
dynamically. I then tried to think how to queue the
deletes to be done after the loop but could not
figure out how to do this.

Colin

"Colin Brown" <cbrown at metservice.com> wrote in message
news:3fa6c327$1 at news.iconz.co.nz...
> My code fragment below:
>
> for key1 in dict1.keys():
>     for key2 in dict1[key1]:
>         if dict1[key1][key2] == None:
>             del dict1[key1][key2]
>
> gives the following error:
> RuntimeError: dictionary changed size during iteration
>
> Any suggestion on how to code this please.
>
> Thanks
> Colin Brown
> PyNZ
>
>
>






More information about the Python-list mailing list