Splitting a list into uneven tuples
<p>I'm trying to split a list of strings into a list of tuples of uneven length containing these strings, with each tuple containing strings initially separated with blank strings. Basically I'd need the parameterized split that I could apply to lists. If my initial list looks like:</p>

<pre><code>init = ['a', 'b', '', 'c', 'd e', 'fgh', '', 'ij', '', '', 'k', 'l', '']
</code></pre>

<p>The last element of this list is always a closing <code>''</code>. There can be consecutive <code>''</code>s which shall be considered as singles.
The result I need is:</p>

<pre><code>end = [('a', 'b'), ('c', 'd e', 'fgh'), ('ij',), ('k', 'l')]
</code></pre>

<p>I already have ugly code that does the job and gets out of range once the list is fully popped out:</p>

<pre><code>end = []
while init[-1] == u'':
 init.pop()
 l = []
 while init[-1] != u'':
  l.append(init.pop())
 end.append(tuple(l))
</code></pre>

<p>I'd like to use comprehensions, but having unsuccessfully tried unpacking argument lists, reversing self-referenced lists, using <code>deque</code> queues, and various code smells, I'm now doubting whether it makes sense looking for a (nested) comprehension solution?</p>

<p>Here is a more concise and general approach with <code>groupby</code> if you want to split your list with a special delimiter:</p>

<pre><code>&gt;&gt;&gt; delimiter = ''
&gt;&gt;&gt; [tuple(g) for k, g in groupby(init, delimiter.__eq__) if not k]
[('a', 'b'), ('c', 'd e', 'fgh'), ('ij',), ('k', 'l')]
</code></pre>

python list python-2.7 group list-comprehension