title: Efficiency difference between dict.has_key and key in dict in Python
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/1323410/has-key-or-in">&#39;has_key()&#39; or &#39;in&#39;?</a>  </p>
  </blockquote>
  
  
  
  <p>In Python, there're two ways of deciding whether a <code>key</code> is in a <code>dict</code>:</p>
  
  <p><code>if dict.has_key(key)</code> and <code>if key in dict</code></p>
  
  <p>Someone tells me that the second one is slower than the first one since the <code>in</code> keyword makes the expression an iteration over the dict, so it will be slower than the <code>has_key</code> alternative, which apparently uses hash to make the decision.</p>
  
  <p>As I highly doubt the difference, since I think Python is smart enough to translate an <code>in</code> keyword before a <code>dict</code> to some hash way, I can't find any formal claim about this.</p>
  
  <p>So is there really any efficiency difference between the two?</p>
  
  <p>Thanks.</p>