0

This must be trivial.

I want to calculate the following.

100*((1 + r)**n), for n = 0, 1, 2, ..., N

I used the following.

>>> list(itertools.accumulate([c0, range(5)], lambda w,r: w*(1.02**r)))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <lambda>
TypeError: unsupported operand type(s) for ** or pow(): 'float' and 'range'```

2
  • 1
    What does c0 represent in your code? Commented Jul 9, 2021 at 1:07
  • And what does r represent in your formula? Your formula looks like it should be a simple list comprehension, like in @AliSoliman's answer. Commented Jul 9, 2021 at 1:16

3 Answers 3

2

One way to do it would be a list comprehension

[100*((1 + r)**n) for n in range(N)]

This would give a list for each value of n, 0 <= n < N

Sign up to request clarification or add additional context in comments.

Comments

2

You need to unpack (star, *) your range:

list(itertools.accumulate([c0, *range(5)], lambda w,r: w*(1.02**r)))

Comments

0

You need the range to build the list of numbers; putting range(5) into the list won't actually build the list:

list1 = [c0] + [i for i in range(5)]

1 Comment

[i for i in range(5)]-> list(range(5))

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.