Safer iteration over dict.keys() in py3

Thanks for the suggestion @Jorge-C!