.keys() returns a list (in python2) so if you write<p><pre><code>  for k in dict.keys():
    ...
</code></pre>
then python first builds a list of all the keys, loops through them and then throws away the list.  If the dict is large, this can be quite expensive.  The correct way is to either use .iterkeys() which returns an iterator which generates the keys one at a time, or simply iterate directly over the dict,  saving you need to first copy all the keys into a list you&#x27;ll just throw away.<p>This has been &#x27;fixed&#x27; in python3 and .keys() now returns an  iterable view of the keys, and if you actually want a list of the keys you have to explicit and write list(dict.keys())