I have the following problem: In a loop over a input parameter list I make an API call for each list entry. I want to then write the results into a dict and append that dict to an output list. At the end of the loop I want to print that output list. What happens is that the last API call / the last element in the input parameter list is overwritten every time I append to the output list.
import requests
input_parameters = ['A', 'B', 'C', 'D']
output = []
record = {}
for input in input_parameters:
r=requests.get("https://api.domain.com/v2/param=" + str(input))
record['id'] = str(r.json()['data'][0]['id'])
record['descr'] = str(r.json()['data'][0]['descr'])
record['qty'] = str(r.json()['data'][0]['qty'])
output.append(record)
print(output)
The output I get:
[{'id': 'D', 'descr': 'foobar', 'qty': '10'}, {'id': 'D', 'descr': 'foobar', 'qty': '10'}, {'id': 'D', 'descr': 'foobar', 'qty': '10'}, {'id': 'D', 'descr': 'foobar', 'qty': '10'}]
The output I would like:
[{'id': 'A', 'descr': 'barfoo', 'qty': '50'}, {'id': 'B', 'descr': 'bar or foo', 'qty': '80'}, {'id': 'C', 'descr': 'foo foo bar', 'qty': '25'}, {'id': 'D', 'descr': 'foobar', 'qty': '10'}]
Your help is much appreciated.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…