title: Compute the cumulative sum of a list until a zero appears
body: <p>I have a (long) list in which zeros and ones appear at random:</p>
  
  <pre><code>list_a = [1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1]
  </code></pre>
  
  <p>I want to get the list_b</p>
  
  <ul>
  <li>sum of the list up to where 0 appears</li>
  <li><p>where 0 appears, retain 0 in the list </p>
  
  <pre><code>list_b = [1, 2, 3, 0, 1, 2, 0, 1, 0, 1, 2, 3]
  </code></pre></li>
  </ul>
  
  <p>I can implement this as follows:</p>
  
  <pre><code>list_b = []
  for i, x in enumerate(list_a):
      if x == 0:
          list_b.append(x)
      else:
          sum_value = 0
          for j in list_a[i::-1]:
              if j != 0:
                  sum_value += j
              else:
                  break
          list_b.append(sum_value)
  print(list_b)
  </code></pre>
  
  <p>but the actual list's length is very long.</p>
  
  <p>So, I want to improve code for high speed. (if it is not readable)</p>
  
  <p>I change the code like this:</p>
  
  <pre><code>from itertools import takewhile
  list_c = [sum(takewhile(lambda x: x != 0, list_a[i::-1])) for i, d in enumerate(list_a)]
  print(list_c)
  </code></pre>
  
  <p>But it is not fast enough. How can I do it in more efficient way?</p>