title: Is checking if key is in dictionary and getting it's value in the same "if" safe?
body: <p>I think this is safe:</p>
  
  <pre><code>if key in test_dict:
      if test_dict[key] == 'spam':
          print('Spam detected!')
  </code></pre>
  
  <p>but is this safe?</p>
  
  <pre><code>if key in test_dict and test_dict[key] == 'spam':
      print('Spam detected!')
  </code></pre>
  
  <p>It should do the same thing because condition checking is lazy in python. It won't try to get the value (and raise an exception because there's no such key in the dict) because the first condition is already not satisfied. But can I rely on the lazyness and use the second example in my programs?</p>