0

I have a series of data and would like to perform the following opperation:

AA = [2, 4, 6, 8]
a = []
a = AA[0]

b = AA[1]
sum_1 = a+b 

c = AA[2]
sum_2 = sum_1 + c

d = AA[3]
sum_3 = sum_2 + d

To make it more concise, I'd like to put it in a for loop, but I can't figure out how.

The desired output for me will be the updated sum_3

Michael Delgado
  • 13,789
  • 3
  • 29
  • 54
Ehsan
  • 89
  • 9
  • 1
    append new values to list `a`. Don't try to define new variables. – hpaulj Feb 14 '23 at 18:31
  • Does this actually have anything to do with numpy or xarray? If so, please edit your example to include real code, not pseudo code. If not, please remove these tags. – Michael Delgado Feb 14 '23 at 18:32
  • Does this answer your question? [How do you create different variable names while in a loop?](https://stackoverflow.com/questions/6181935/how-do-you-create-different-variable-names-while-in-a-loop) – Michael Delgado Feb 14 '23 at 18:33

1 Answers1

0

That will do it for you:

AA = [2, 4, 6, 8]
s = 0
for a in AA:
    s += a
print(s)
20

Cheers

Michael
  • 2,167
  • 5
  • 23
  • 38