Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

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
 
Share this answer
 
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)
 
Share this answer
 
Comments
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)



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