Wednesday, 14 August 2013

Adding one element from a list to next,after that to next etc

Adding one element from a list to next,after that to next etc

Here is a example from a simple list
mylist = [2,5,9,12,50]
Sorry for my horrible grammar i try my best,so do not be so rude pls. I
want to add the first element,in this case 2,to the element next to
it.It's the number 5. The outcome(2+5=7)should be added to the next
element.Number 9 in my example.The outcome added to element next etc...
I have this snippet right now it's working,but they must be a simpler and
better way:
newlist = [5, 9, 12 , 50]
counts = 0
a = 2
while (counts < 5):
a = a + mylist[n]
print a
counts = counts + 1
output is:
7
16
28
78

No comments:

Post a Comment