Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enumurate values from a list and then sum in a function?

Tags:

python

I'm trying to create a function that uses the value and the index of the value, starting from 1 to put the values in a function and sums the whole list.

In other words I'm trying to get the present value of a list of cashflows that are not the same, therefor have to calculate the present value of each cash flow and sum them.

This is what I have tried.

cash = [10,10,10,10, 10, 10, 11, 10]
DiscoutRate = 0.12
    for (period, value) in enumerate(cash, start=1):
        PV = value/((1+DiscountRate)**period)
        print(PV)

But I see that the results are not correct, any thoughts?

like image 762
MisterButter Avatar asked Jan 27 '23 06:01

MisterButter


1 Answers

Note that this solution uses a Generator Expression.

>>> sum(value/((1 + DiscountRate) ** period) for period, value in enumerate(cash, start=1))
50.1287
like image 65
Alexander Avatar answered Jan 29 '23 21:01

Alexander