title: I have a list of numbers that are sometimes consecutive - how to get a list of ranges from it?
body: <h2><em>Motivation:</em></h2>
  <p>A list of <code>[1,2,3,4,5,6,7,8,9,10]</code> is expressible as <code>range(1,11)</code> and <code>in</code> checks are near <a href="https://stackoverflow.com/a/30081318/7505395">constant time computations</a> if checked against ranges. Big-ish lists of numbers can be simplified if expressed as list of <code>range()</code> objects using fewer bytes then used by storing all of them as numbers in a list.</p>
  <pre><code># what I have:
  numbers = [1, 2, 5, 7, 8, 9, 10, 13, 14, 15, 42, 43, 44, 45, 46, 47, 48, 49, 50] 
  
  # what I want:
  ranges = [range(1,3), range(5,6), range(7,11), range(13,16), range(42,51)]
  </code></pre>
  <h2><em>(Self answered) Question</em>:</h2>
  <p>How can I move from a list of numbers to a list of ranges to facilitate smaller storage and faster <code>in</code> checks - for consecutive numbers?</p>
  <hr />
  <p>Inspired by</p>
  <ul>
  <li><a href="https://stackoverflow.com/questions/72042054/find-index-of-longest-continious-sequence-in-list-python">Find index of longest continious sequence in list python</a></li>
  <li><a href="https://stackoverflow.com/questions/29976532/how-to-transform-a-list-of-numbers-into-a-list-of-ranges-of-consecutive-numbers">How to transform a list of numbers into a list of ranges of consecutive numbers</a> (which would take the ranges and string-format them)</li>
  <li>my answer to <a href="https://stackoverflow.com/questions/72012021/concatenate-and-simplify-a-list-containing-number-and-letter-pairs/72012246#72012246">Concatenate and simplify a list containing number and letter pairs</a> where I could have used this to merge  consecutive <code>ord(letter)</code>'s of <code>&quot;abcd&quot;</code> into <code>&quot;a-d&quot;</code></li>
  </ul>