How to put sequential numbers into lists from a list
<p>I have a list numbers say,</p>

<p><code>[1,2,3,6,8,9,10,11]</code></p>

<p>First, I want to get the sum of the differences (step size) between the numbers <code>(n, n+1)</code> in the list.</p>

<p>Second, if a set of consecutive numbers having a difference of 1 between them, put them in a list, i.e. there are two such lists in this example,</p>

<p><code>[1,2,3]</code></p>

<p><code>[8,9,10,11]</code></p>

<p>and then put the rest numbers in another list, i.e. there is only one such list in the example,</p>

<p><code>[6]</code>.</p>

<p>Third, get the lists with the max/min sizes from the sequential lists, i.e. <code>[1,2,3]</code>, <code>[8,9,10,11]</code> in this example, the max list is,</p>

<p><code>[8,9,10,11]</code></p>

<p>min list is</p>

<p><code>[1,2,3]</code>.</p>

<p>What's the best way to implement this?</p>

<blockquote>
  <p>First, I want to get the sum of the differences (step size) between
  the numbers <code>(n, n+1)</code> in the list.</p>
</blockquote>

<p>Use <code>sum</code> on the successive differences of elements in the list:</p>

<pre><code>&gt;&gt;&gt; sum(lst[i] - x for i, x in enumerate(lst[:-1], start=1))
10
</code></pre>

<hr>

<blockquote>
  <p>Second, if a set of consecutive numbers having a difference of 1 between them, put them in a list, i.e. there are two such lists in
  this example, and then put the rest numbers in another list, i.e.
  there is only one such list in the example,</p>
</blockquote>

<p><a href="https://docs.python.org/2/library/itertools.html#itertools.groupby" rel="nofollow"><code>itertools.groupby</code></a> does this by grouping on the difference of each element on a reference <a href="https://docs.python.org/2/library/itertools.html#itertools.count" rel="nofollow"><code>itertools.count</code></a> object:</p>

<pre><code>&gt;&gt;&gt; from itertools import groupby, count
&gt;&gt;&gt; c = count()    
&gt;&gt;&gt; result = [list(g) for i, g in groupby(lst, key=lambda x: x-next(c))]
&gt;&gt;&gt; result
[[1, 2, 3, 4], [6], [8, 9, 10, 11]]
</code></pre>

<hr>

<blockquote>
  <p>Third, get the lists with the max/min sizes from above</p>
</blockquote>

<p><code>max</code> and <code>min</code> with the <em>key function</em> as <code>sum</code>:</p>

<pre><code>&gt;&gt;&gt; max(result, key=sum)
[8, 9, 10, 11]
&gt;&gt;&gt; min(result, key=sum)
[6] #??? shouldn't this be [6]
</code></pre>

python list