0

I have a dictionary with a list that appears like this:

{'subscription_id_list': ['92878351', '93153640', '93840845', '93840847'.......]

There will be up to 10,000 entries in the list for each call to the API. What I want to do is loop and append the list so when complete, my {'subscription_id_list'} contains everything.

Any thoughts?

Landon Statis
  • 683
  • 2
  • 10
  • 25
  • A few more details: you're making calls to an API based on the contents of a list? Or you're making calls to an API and want a list to contain the responses? What does your `{'subscription_id_list'}` contain at the start, and from what do the results come to add to it? – Joshua Voskamp Nov 10 '21 at 22:53
  • From my limited understanding, is something like [this](https://stackoverflow.com/questions/9110593/asynchronous-requests-with-python-requests) relevant to what you want? – Joshua Voskamp Nov 10 '21 at 22:54
  • The result from the call is just like that: {'subscription_id_list': ['92878351', '93153640', '93840845', '93840847'.......]} So, after making X number of calls, each one returning this same structure, in the end I'd like a single dictionary list with all the elements. So, {'subscription_id_list': ['92878351', '93153640', '93840845', '93840847', '12345','123455','666','888','999','000','443242',..............} And so on, might be 100,000 entries in the list after everything is appended. Just not sure how to do the appending. – Landon Statis Nov 10 '21 at 22:56
  • How's that? you make multiple calls, each returning you a list? And you want to combine all those lists? Could you provide a [minimum reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) (abstracting away the not-relevant stuff as you see fit)? – Joshua Voskamp Nov 10 '21 at 22:57
  • Possibly also relevant if you haven't already considered it: [asynchronous http requests in python](https://www.twilio.com/blog/asynchronous-http-requests-in-python-with-aiohttp) – Joshua Voskamp Nov 10 '21 at 22:58

1 Answers1

0

It sounds like you have something like

for api_request in some_structure:
    response = get_remote_results(api_request)
    # response structure is single-key dict,
    # key is 'subscription_id_list'; value is list of str of int
    # i.e. {'subscription-id-list': ['1234','5678',....]}

and you want some complete dict to contain the list of all values in the various responses. Then use list.extend:

complete_sub_id_list = []
for api_request in some_structure:
    response = get_remote_results(api_request)
    complete_sub_id_list.extend(response['subscription_id_list'])

As noted in a comment, if you're making API requests to a remote server or resource (or even something dependent on local I/O), you're likely going to want to use asynchronous calls; if you're not already familiar with what that means, perhaps aiohttp and/or asyncio will be quite helpful. See e.g. this Twilio guide for more info. TL;DR you could see 5-30x speedup as opposed to using synchronous calls.

Joshua Voskamp
  • 1,855
  • 1
  • 10
  • 13