can't delete from a dictionary in a loop

bruno.desthuilliers at gmail.com bruno.desthuilliers at gmail.com
Fri May 16 17:34:58 EDT 2008


On 16 mai, 23:28, Hans Nowak <zephyrfalcon!NO_SP... at gmail.com> wrote:
> Dan Upton wrote:
> > for pid in procs_dict:
> >   if procs_dict[pid].poll() != None
> >    # do the counter updates
> >    del procs_dict[pid]
>
> > The problem:
>
> > RuntimeError: dictionary changed size during iteration
>
> I don't know if the setup with the pids in a dictionary is the best way to
> manage a pool of processes... I'll leave it others, presumably more
> knowledgable, to comment on that. :-)  But I can tell you how to solve the
> immediate problem:
>
>    for pid in procs_dict.keys():

I'm afraid this will do the same exact thing. A for loop on a dict
iterates over the dict keys, so both statements are strictly
equivalent from a practical POV.



More information about the Python-list mailing list