Proper way to split Python list into list of lists on matching list item delimeter
<pre><code>list = ['a', 'b', 'c', '', 'd', 'e', 'f', '', 'g','h','i']

def chunk(list, delim):
    ct = list.count(delim)
    chunks = [[]] * (ct+1)
    for iter in range(ct):
        idx = list.index(delim)
        chunks[iter] = list[:idx]
        list = list[idx+1:]
    chunks[ct] = list
    return chunks

print chunk(list, '')
</code></pre>

<p>Produces:</p>

<pre><code>[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
</code></pre>

<p>Which is what I want, but I feel like this is the 'c' way to accomplish this. Is there a more pythonistic construct I should know? </p>

<p>Here is one way to do it <a href="https://docs.python.org/dev/library/itertools.html#itertools.groupby" rel="nofollow">using <code>itertools.groupby</code></a></p>

<pre><code>[list(v) for k, v in groupby(l, key=lambda x: x!= '') if k]
</code></pre>

<p>Demo:</p>

<pre><code>&gt;&gt;&gt; from itertools import groupby
&gt;&gt;&gt; l = ['a', 'b', 'c', '', 'd', 'e', 'f', '', 'g','h','i']
&gt;&gt;&gt; ch = [list(v) for k, v in groupby(l, key=lambda x: x!= '') if k]
&gt;&gt;&gt; ch
[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
</code></pre>

python