Reputation: 61
I apologize in advance for misused terminology and description, but I'm able to describe it well enough:
I'm making requests to an API, while using a 'for loop' to iterate through a list of subdirectories I've identified for a domain.
responses = list() ##defining that responses is a list
responses.clear() ##clearing the list each time I run the script
ids = ['XXXXXXid1XXXXXX', 'XXXXXXid2XXXXXX', 'XXXXXXid3XXXXXX'] ##defining list of ids
for id in ids:
url = 'https://api.website/PartOfWebsite/{}'.format(id)
rr = requests.get(url, params= {'apikey': 'my-api-key'})
data = json.loads(rr.text)
responses.append(data)
When looking at 'responses' after this succesffully runs, the results look like:
[{'python_rules': [{'ad': 'some result',
'ret': 23908092093094808203,
'shure': 2.5},
{'ad': 'some result',
'ret': 902830894023342,
'share': 2.32},
{'ad': 'some result',
'ret': 98209283049820,
'shure': 21.3}]
I would like the results to include the id that is associated to the search, in the 'ids' list. So, the results would look like:
[{'python_rules': [{'ad': 'some result',
'ret': 23908092093094808203,
'shure': 2.5,
'id': XXXXXXid1XXXXXX},
{'ad': 'some result',
'ret': 902830894023342,
'share': 2.32
'id': XXXXXXid2XXXXXX},
{'ad': 'some result',
'ret': 98209283049820,
'shure': 21.3
'id': XXXXXXid3XXXXXX}]
Please let me know if I need to be more specific about what I'm looking for. Thanks!
Upvotes: 0
Views: 45
Reputation: 1320
You can simply add the id manually to the resulting data
dictionary:
responses = list() ##defining that responses is a list
responses.clear() ##clearing the list each time I run the script
ids = ['XXXXXXid1XXXXXX', 'XXXXXXid2XXXXXX', 'XXXXXXid3XXXXXX'] ##defining list of ids
for id in ids:
url = 'https://api.website/PartOfWebsite/{}'.format(id)
rr = requests.get(url, params= {'apikey': 'my-api-key'})
data = json.loads(rr.text)
data['id'] = id # <-- add id manually
responses.append(data)
Upvotes: 1