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>Here is an option with a <code>for</code> loop:</p>

<pre><code>zeros = 0
result = [0]

for i in lst:
    if i == 0:
        zeros += 1

    elif zeros &gt;= 2:                # if there are more than two zeros, append the new 
                                    # element to the result
        result.append(i)
        zeros = 0

    else:                           # Otherwise add it to the last element
        result[-1] += i
        zeros = 0

result
# [-7, -8, -4, -65, 20, -3]
</code></pre>

<p>To get the corresponding index, you can use <code>enumerate</code>:</p>

<pre><code>zeros = 0
reSum = [0]
reInd = [[]]
for i, v in enumerate(lst):
    if v == 0:
        zeros += 1
    elif zeros &gt;= 2:
        zeros = 0
        reSum.append(v)
        reInd.append([i])
â
    else:
        zeros = 0
        reSum[-1] += v
        reInd[-1] += [i]

reSum
# [-7, -8, -4, -65, 20, -3]

reInd
# [[1],
#  [4],
#  [7],
#  [11, 13, 14, 15, 17],
#  [20, 21, 23, 24, 25],
#  [32, 33, 34, 36, 37, 38, 39]]
</code></pre>

python python-2.7