create sub lists by grouping with conditional check on two consecutive zeros
<p>I have a list:</p>

<pre><code>lst = [0, -7, 0, 0, -8, 0, 0, -4, 0, 0, 0, -6, 0, -4, -29, -10, 0, -16, 0, 0, 2, 3, 0, 18, -1, -2, 0, 0, 0, 0, 0, 0, 21, 10, -10, 0, -12, 3, -5, -10]
</code></pre>

<p>I want to create group of sublists with conditional break when a value is followed by two consecutive zeros.</p>

<p>so my intermediate list would like </p>

<pre><code>newlst = [-7,-8,-4,[-6,-4,-29,-10,-16],[2,3,18,-1,-2],[21,10,-10,-12,3,-5,-10]]
</code></pre>

<p>whereas the final output will be sum of sublists:</p>

<pre><code>[-7,-8,-4,-65,18,-3]
</code></pre>

<p>I tried using the index number in a for loop with enumerate but i'm not getting my desired output.</p>

<p>Cheesy solution if your values all occur in <code>range(-128, 128)</code> is to use the <code>split</code> and <code>replace</code> methods of <code>bytearray</code> (which conveniently converts to and from <code>int</code>) to do the splitting:</p>

<pre><code>lst = [0, -7, 0, 0, -8, 0, 0, -4, 0, 0, 0, -6, 0, -4, -29, -10, 0, -16, 0, 0, 2, 3, 0, 18, -1, -2, 0, 0, 0, 0, 0, 0, 21, 10, -10, 0, -12, 3, -5, -10]

# Adjust all  values up by 0x80 so they fall in bytearray's expected 0-255 range
bts = bytearray(x + 0x80 for x in lst)

# Split on double \x80 (the value of 0 after adjustment)
# then remove single \x80 from each part
parts = [b.replace(b'\x80', b'') for b in bts.split(b'\x80\x80')]

# Undo adjustment and unpack (and filter empty sublists out completely)
newlst = [b[0] - 0x80 if len(b) == 1 else [x - 0x80 for x in b] for b in parts if b]

# Or to just get the sums, no need to create newlst, just filter and sum en masse:
sums = [sum(b) - 0x80 * len(b) for b in parts if b]
</code></pre>

python python-2.7