Remove redundant use of .keys()

When iterating over a dictionary, or checking for membership of a key in a dictionary, the canonical form is:

for key in dict:
    ...

if key in dict:
    ...

This PR cleans up code which uses "dict.keys()" rather than "dict" in these cases.