Python: split list into indices based on consecutive identical values
<p>If you could advice me how to write the script to split list by number of values I mean: </p>

<pre><code>my_list =[11,11,11,11,12,12,15,15,15,15,15,15,20,20,20] 
</code></pre>

<p>And there are 11-4,12-2,15-6,20-3 items.
 So in next list for exsample range(0:100) 
I have to split on 4,2,6,3 parts 
So I counted same values and function for split list, but it doen't work with list:</p>

<pre><code> div=Counter(my_list).values() ##counts same values in the list

 def chunk(it, size):
    it = iter(it)
    return iter(lambda: tuple(islice(it, size)), ())
</code></pre>

<p>What do I need:</p>

<pre><code>Out: ([0,1,2,3],[4,5],[6,7,8,9,10,11], etc...] 
</code></pre>

<p>You can use <a href="https://docs.python.org/2/library/functions.html#enumerate" rel="nofollow"><code>enumerate</code></a>, <a href="https://docs.python.org/2/library/itertools.html#itertools.groupby" rel="nofollow"><code>itertools.groupby</code></a>, and <a href="https://docs.python.org/2/library/operator.html" rel="nofollow"><code>operator.itemgetter</code></a>:</p>

<pre><code>In [45]: import itertools

In [46]: import operator

In [47]: [[e[0] for e in d[1]] for d in itertools.groupby(enumerate(my_list), key=operator.itemgetter(1))]
Out[47]: [[0, 1, 2, 3], [4, 5], [6, 7, 8, 9, 10, 11], [12, 13, 14]]
</code></pre>

<p>What this does is as follows:</p>

<ol>
<li><p>First it enumerates the items.</p></li>
<li><p>It groups them, using the second item in each enumeration tuple (the original value).</p></li>
<li><p>In the resulting list per group, it uses the first item in each tuple (the enumeration)</p></li>
</ol>

python list split