RuntimeError: dictionary changed size during iteration

  • In Python 2.x calling keys makes a copy of the key that you can iterate over while modifying the dict:

    for i in d.keys():
    
  • Note that this doesn't work in Python 3.x because keys returns an iterator instead of a list.
    Another way is to use list to force a copy of the keys to be made. This one also works in Python 3.x:

    for i in list(d):
    

Reference

你可能感兴趣的:(RuntimeError: dictionary changed size during iteration)