title: Compare list of datetimes with datetime in Python
body: <p>I have a list of datetime objects and would like to find the ones which are within a certain time frame:</p>
  
  <pre><code>import datetime
  
  dates = [ datetime.datetime(2007, 1, 2, 0, 1),
            datetime.datetime(2007, 1, 3, 0, 2),
            datetime.datetime(2007, 1, 4, 0, 3),
            datetime.datetime(2007, 1, 5, 0, 4),
            datetime.datetime(2007, 1, 6, 0, 5),
            datetime.datetime(2007, 1, 7, 0, 6) ]
  #in reality this is a list of over 25000 dates
  
  mask = (dates&gt;datetime.datetime(2007,1,3)) &amp; \
         (dates&lt;datetime.datetime(2007,1,6))
  </code></pre>
  
  <p>However, this results in the following error:
  "TypeError: can't compare datetime.datetime to list"</p>
  
  <p>How can I fix my code?</p>