Remove usage of .keys(), .has_key(), and .iteritems(), and account for the fact that .values() produces an iterator in Python 3.