15,958,239 members
See more: , +
how to do a sum in the list, for example [3,0,1,-2,] return [3,3,4,2], adding index by index

What I have tried:

Python
```list = [3,0,1,-2]
total = 0
for i in list:
total = total + i

print(lista)```
Posted
Updated 24-Jul-22 21:37pm
v2

## Solution 2

Python
```list = [3,0,1,-2]

for i in range(1, len(list)):
list[i] += list[i - 1]

print(list)```

Produces:
```python atest.py
[3, 3, 4, 2]

Python test result: 0```

## Solution 1

Python
```list = [3,0,1,-2]
total = 0
# you may need to create lista here
for i in list:
total = total + i
# add total to lista here
print(lista)```

CPallini 25-Jul-22 2:11am
5.
Patrice T 25-Jul-22 4:17am
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 OriginalGriff 28 Pete O'Hanlon 10 Member 13798091 -6 SG Tamilan -8
 Pete O'Hanlon 70 OriginalGriff 60 merano99 20 WowDef 10 esteenbrink 10

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900