How to iterate on a changing dictionary

TheSaint nobody at nowhere.net.no
Sun Jun 19 10:32:36 EDT 2011


Hello

Trying to pop some key from a dict while is iterating over it will cause an 
exception.
How I can remove items when the search result is true.

Example:

while len(dict):
   for key in dict.keys():
      if dict[key] is not my_result:
         dict.pop(key)
    else:
       condition_to_break
print('Dictionary is over')

this is my mistake, but where to fix?
-- 
goto /dev/null



More information about the Python-list mailing list