I am trying to sum a list by values in python and i am not having any luck
<p>Good day,</p>

<p>I am trying to sum a list by values in python and i am not having any luck. what i would like to end up with is three summed values.</p>

<p>lst = [100, -1, -2, -3, 100, -1, -2, -3, 100]</p>

<p>100 -1-2-3 = 94</p>

<p>100 -1-2-3 = 94</p>

<p>100 = 100</p>

<p>Any help you be greatly appreciated.</p>

<p>Thanks</p>

<p>How about this?</p>

<pre><code>s = []
for i in lst:
    if i &gt; 0 or len(s) == 0:        
        s.append(i)     # start a new element in s when element is larger than zero
    else:
        s[-1] += i      # otherwise add it to the last element of s

s
# [94, 94, 100]
</code></pre>

<p>Or if you use pandas:</p>

<pre><code>import pandas as pd:
ser = pd.Series(lst)
ser.groupby((ser &gt; 0).cumsum()).sum()

#1     94
#2     94
#3    100
#dtype: int64
</code></pre>

python list sum group