0
Example:
request = requests.get('https://api.amountofsomething.com')
amount = request.json()['amount']
This request returns values like these:
[{"amount": 0.1}, {"amount": 2},{"amount": 145},{"amount": 5.84}]
I need to do sums, without patterns, of different index values. I have done so, and it has worked, but for large amounts of data the code gets very extensive.
amount0 = amount[0]['amount']
amount1 = amount[1]['amount']
amount2 = amount[2]['amount']
sum0 = amount0 + amount2
>>> sum0 = 0.1 + 145
...
I tried in different ways, to manipulate this data to generate less code.
sum0 = amount [0+2]['amount']
sum1 = amount [[0] + [2]]['amount']
sum2 = amount [0]['amount'] + amount [2]['amount']
None of those options worked, can anyone give me a hand ?
First of all, thank you for answering. With this code I get the return of the sum of all quantities, but in case I need to add for example the first 15 quantities , I would have to modify your code for this ?
– Eliezer Borges
Changed to your suggestion. Just added index count..
– Marlysson