Iterating through a dictionary to get next values from previous values

Advertisements

I have this sample dataset

{'data1':200, 'data2':500}

I want to iterate this data in a range of 25, such that each iteration would give me the previous value * (1+0.05) within the iteration.

In this case the output of range of 2 would look like this:

{'data1':[200,210], 'data2':[500, 525]}

Anyone has an idea of how to go about this?

>Solution :

dict = {'data1':200, 'data2':500}
e= {}
for key, value in dict.items():
    v = value
    tab = []
    for i in range(25):
        tab.append(v*(1.05)**i)
    e[(key)]=tab

print(e)

OUTPUT :

{'data1': [200.0, 210.0, 220.5, 231.52500000000003, 243.10125000000005, 255.25631250000006, 268.01912812500007, 281.4200845312501, 295.4910887578126, 310.26564319570326, 325.7789253554884, 342.0678716232628, 359.171265204426, 377.12982846464735, 395.98631988787974, 415.78563588227377, 436.5749176763874, 458.40366356020684, 481.3238467382171, 505.3900390751281, 530.6595410288844, 557.1925180803287, 585.0521439843452, 614.3047511835624, 645.0199887427406], 'data2': [500.0, 525.0, 551.25, 578.8125000000001, 607.7531250000001, 638.1407812500001, 670.0478203125002, 703.5502113281252, 738.7277218945316, 775.6641079892581, 814.447313388721, 855.1696790581572, 897.9281630110651, 942.8245711616183, 989.9657997196994, 1039.4640897056843, 1091.4372941909685, 1146.009158900517, 1203.309616845543, 1263.4750976878202, 1326.6488525722111, 1392.9812952008217, 1462.630359960863, 1535.761877958906, 1612.5499718568515]}

Leave a Reply Cancel reply