...
for self.key2 in self.citysetdict[self.key]:
....
if self.compare.dist<=distancethreshold:
...
self.temp = self.citysetdict[self.key]
self.temp[self.city[apiad]]=self.city
This looks like your problem. You're iterating over the dictionary self.citysetdict[self.key]
. Then you assign self.citysetdict[self.key]
to self.temp
, and then assign an item of self.temp
(which will change its size unless the key already existed).
As an aside, when Python reported that error about "dictionary size changed during iteration", it would have told you exactly which line was the problem. That clue could have saved your confusion about how you thought you weren't changing size until after the for loops were finished, because it would clearly have pointed to a line within the for loops (which also greatly narrows down which dictionary is the problem). But even if you don't yet know how to read the stack traces printed in the error message to do this kind of problem identification (which is fine, everyone has to learn sometime), when you're asking others for help please include the error message, so that the people helping you can make use of this clue!