How to sum up each element in array list?
<p>Having a bit of writing out the code. </p>

<p>For example, if I have an array of: </p>

<pre><code>a = ([0, 0, 1, 2], [0, 1, 1, 0], [0, 0, 1, 0], [1, 0, 1, 3], [0, 1, 1, 3])
</code></pre>

<p>if I want to add first element of each item, </p>

<p>as in to return a list of 0 + 0 + 0 + 1 + 0, 0 + 1 + 0, 0 + 0 ... </p>

<p>I wrote the code: </p>

<pre><code>def test(lst):
  sum = 0
  test_lst = [] 
  i = 0
  while i in range(0, 4):
    for j in range(0, len(lst)):
      sum += lst[j][i]
      test_lst.append(sum)
      i += 1 
 return test_lst
</code></pre>

<p>I get index size error.
How can I go about this?</p>

<pre><code>a = ([0, 0, 1, 2], [0, 1, 1, 0], [0, 0, 1, 0], [1, 0, 1, 3], [0, 1, 1, 3])
</code></pre>

<p>Try using list comprehensions:</p>

<pre><code>sum([item[0] for item in a])
</code></pre>

<p>The line above takes the first element of each list in the tuple, then puts it into a temporary list. We then call sum on that temporary list, which yields the answer. </p>

python arrays list