title: Using bisect for combing items with a distance condition
body: <p>In the following two lists</p>
  
  <pre><code> l1 = [10, 33, 50, 67]
   l2 = [7, 16, 29, 65]
  </code></pre>
  
  <p>I use <code>bisect</code> to combine the closest numbers in the two lists. I use this code</p>
  
  <pre><code>for s in l1:
      ind = bisect(l2, s, hi=len(l2) - 1)
      ind -= abs(l2[ind-1] - s) &lt; l2[ind] - s
      print("{} -&gt; {}".format(s, l2[ind]))
  </code></pre>
  
  <p>This code gives the output:</p>
  
  <pre><code>10 -&gt; 7
  33 -&gt; 29
  50 -&gt; 65
  67 -&gt; 65
  </code></pre>
  
  <p>In order to get rid of the duplication of items in the second list, I used this after the print:</p>
  
  <pre><code>    if ind == len(l2) - 1: break  
  </code></pre>
  
  <p>But in the case of if statement the output is:</p>
  
  <pre><code>10 -&gt; 7
  33 -&gt; 29
  50 -&gt; 65
  </code></pre>
  
  <p>What I want here is that the combination will be done with the condition that the distance between the closest numbers are not greater than 6, but without breaking the loop for items that fail this test, as in the case of my last code. I want this output:</p>
  
  <pre><code>10 -&gt; 7
  33 -&gt; 29
  50 -&gt; -- # here for the condition
  67 -&gt; 65
  </code></pre>