The easiest is to never use .keys() or .iterkeys(), and always iterate over the bare dict:<p><pre><code>    for k in dict:
        ...

    if k in dict:
        ...
</code></pre>
If you do need a list of keys, list(dict) has the advantage of working in both Python 2 and 3.