ubuntu_noobie
ubuntu_noobie

Reputation: 33

Add previous element to current in list then assign to variable - Python 3.5.2

First post on here, I'm stoked! Okay, so here is the question: How do I add previous element to current element and then make a new list from those values to add them to matplotlib graph? Here is an example of my code:


example_list = [20, 5, 5, -10]

print(example_list) '''first output should be'''
[20, 5, 5, -10]

value1 = (example_list[0])
value2 = (example_list[0] + example_list[1])
value3 = (example_list[0] + example_list[1] + example_list[2])
value4 = (example_list[0] + example_list[1] + example_list[2] + example_list[3])

'''I think you can see the point I'm trying to make'''

del example_list[:]

example_list.append(value1)
example_list.append(value2)
example_list.append(value3)
example_list.append(value4)

print(example_list)
'''output should be:'''

[20, 25, 30, 20]

'''This works for hard coding it in, but I want this to happen for hundreds of elements. (when people add to list from my "simple_finance_app" Python script)''' Thanks for the help! (P.S. I already have the matplotlib code so no need to add that, however, I it could help others who read this question/answer)

Upvotes: 3

Views: 107

Answers (3)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477607

You can use the itertools.accumulate method for that:

from itertools import accumulate
from operator import add

result = list(accumulate(example_list,add))

This generates:

>>> list(accumulate(example_list,add))
[20, 25, 30, 20]

If the function (here operator.add) works in O(n), then accumulate will work in O(n) whereas your hardcoded solution works in O(n2).

Basically the accumulate(iterable[,func]) function takes as input an iterable [x1,x2,...,xn] and a function f, and each iteration it calls f on the accumulator and the new element. In the first step it emits the first element, and then the accumulator becomes that element. So it basically generates [x1,f(x1,x2),f(f(x1,x2),x3),...]. But without calculating f(x1.x2) each time. It is thus semantically equivalent to:

def accumulate(iterable,func): # semantical equivalent of itertools.accumulate
    iterer = iter(iterable)
    try:
        accum = next(iterer)
        while True:
            yield accum
            accum = func(accum,next(iterer))
    except StopIteration:
        pass

but probably less error prone and more efficient.

Upvotes: 2

timgeb
timgeb

Reputation: 78780

Vanilla for loop solution:

>>> example_list = [20, 5, 5, -10]
>>> out = []
>>> sum = 0
>>> for x in example_list:
...     sum += x
...     out.append(sum)
... 
>>> out
[20, 25, 30, 20]

Note that repeatedly calling sum in a for loop/comprehension will result in O(n^2) complexity.

Upvotes: 0

DeepSpace
DeepSpace

Reputation: 81684

You mean sum combined with slicing?

li = [1, 2, 3, 4]

a = sum(li[:1])
b = sum(li[:2])
c = sum(li[:3])
d = sum(li[:4])  # same as sum(li) in this case

Or in general:

li = [1, 2, 3, 4, 5, 6, 7, 8, 9]

for i in range(len(li)):
    print(sum(li[:i + 1]))
# 1
# 3
# 6
# 10
# 15
# 21
# 28
# 36
# 45

Upvotes: 1

Related Questions