Gonçalo Peres
Gonçalo Peres

Reputation: 13582

Grab and pass dict/JSON associated with string from list

I have the following list

configuration = ['configuration1A1', 'configuration1B1', 'configuration1C1']

Each element of the list is a string

>>>type(configurations_new[0])
str

If I print each string from the list separately, each has associated with it a dictionary

>>>configuration1A1
>>>type(configuration4A3)
dict

As I want to pass the dictionary associated with each string, depending on the name associated with an id on the list above, how do I do that?

So far, I have the following

uuids[i] = api_client.prediction_build_model_predict(globals()[datasets[idx]], configuration[idx], wait_to_finish = False).request_uuid

But the above configuration[idx] is picking up the string with the configuration name and not the dictionary associated with that string. It is required to go to the list and pick up the configuration names (the list is sorted according to the usage one intends to give to it, that's why in what one has it picks up the idx from the list).

And as it picks the string the request to the API looks like the following

enter image description here

Instead of

enter image description here

Upvotes: 0

Views: 33

Answers (1)

Timothy Helton
Timothy Helton

Reputation: 357

If you just add globals() in front of configuration[idx] do you get the desired output?

uuids[i] = api_client.prediction_build_model_predict(
    globals()[datasets[idx]],
    globals()[configuration[idx]],
    wait_to_finish = False).request_uuid


Edit 1:

To avoid the globals() call you could iterate through the datasets and configurations. Something like this:

datasets = [dataset1A1, dataset1B1, dataset1C1]
configuration = [configuration1A1, configuration1B1, configuration1C1]
    
for d_set, config in zip(datasets, configuration):
    uuids.append(
        api_client.prediction_build_model_predict(d_set, config, wait_to_finish=False)
        .request_uuid)

Upvotes: 2

Related Questions