Python: getting the previous item from a list based on closeness
<p>Is there a way to get the previous item in a <code>list</code>, based on 'closeness'?  For example:</p>

<pre><code>[     Range(start=datetime.datetime(2012, 1, 1, 0, 0, 0), end=datetime.datetime(2012, 1, 15, 0, 0, 0)),
      Range(start=datetime.datetime(2012, 2, 1, 0, 0, 0), end=datetime.datetime(2012, 2, 15, 0, 0, 0)),
      Range(start=datetime.datetime(2012, 3, 1, 0, 0, 0), end=datetime.datetime(2012, 3, 15, 0, 0, 0))]
</code></pre>

<p>Above is a <code>list</code> containing <code>namedtuple</code>s (called <code>Range</code>), each containing a start date and an end date.</p>

<p><strong>I know that this can be done in a linear way using loops etc., but I was wondering if it could be done more efficiently?</strong></p>

<p><em>Example input/output</em>:</p>

<p>If I input <code>datetime.datetime(2012, 1, 3, 0, 0, 0)</code>, it should return the closest previous start datetime:</p>

<pre><code>Range(start=datetime.datetime(2012, 1, 1, 0, 0, 0), end=datetime.datetime(2012, 1, 15, 0, 0, 0))
</code></pre>

<p>If I input <code>datetime.datetime(2012, 1, 27, 0, 0, 0)</code>, it should return the same as above:</p>

<pre><code>Range(start=datetime.datetime(2012, 1, 1, 0, 0, 0), end=datetime.datetime(2012, 1, 15, 0, 0, 0))
</code></pre>

<p>If I input <code>datetime.datetime(2012, 2, 14, 0, 0, 0)</code>, it should return:</p>

<pre><code>Range(start=datetime.datetime(2012, 2, 1, 0, 0, 0), end=datetime.datetime(2012, 2, 15, 0, 0, 0))
</code></pre>
