How to save the for loop output containing multiple lists, which are various responses of an api call that i made using a function in python?

Viewed 49

I am using an api to collect data using multiple inputs. I used a for loop to make multiple api requests by iterating through the input values that i already declared . The api call was made using a user defined function. Here response_1 is the inputs of the api call.

for auto_sort in response_1:
    countryname= auto_sort['Country']    
    iso=auto_sort['ThreeLetterSymbol']    
    response_3= api_call(countryname,iso)

I am getting the function output as a list of dictionary with each iteration.

I want to save each iteration output individually or combine all the iterations (lists) into one list.

Need help figuring out how this can be done or basically how can i handle the function output in this case. I am able to access the most recent iteration

1 Answers

You are almost there. Just use a list and in each iteration append the response of the api call to that list.

Example code:


all_responses = []

for auto_sort in response_1:
    countryname= auto_sort['Country']    
    iso=auto_sort['ThreeLetterSymbol']    
    response_3= api_call(countryname,iso)

    all_responses.append(response_3)

Related