title: For comparisons in Python, why is using IN faster than looping ==?
body: <p>I'm trying to understand what is going on under the hood a bit more in Python.  I have two snippets of code, each having list1 and list2 as variables containing a list of integers:</p>
  <h1>Snippet 1:</h1>
  <pre><code>for x in list1:
      for y in list2:
          if x == y: True
  </code></pre>
  <h1>Snippet 2:</h1>
  <pre><code>for y in list2:
      if y in list1: True
  </code></pre>
  <p>Both of them compare list 1 and list 2 and tell us whether they have anything in common, but snippet 2 executes about twice as fast as snippet 1, and I am trying to understand why.</p>
  <p>What is actually going on when using if y <em>in</em> list, which examines each element and determines if they match, as opposed to looping through each element for comparison, that makes it so much faster?</p>