Avoid mutating a dict during fast enumeration

Fixes #1843

Pretty obvious really, with the crash log ;-)