1

I have an API response for listing out information of all Volumes. I want to loop through the response and get the value of the name and assign each one of them dynamically to each url.

This is my main API endpoint which returns the following:

[{'source': None, 'serial': '23432', 'created': '2018-11- 
12T04:27:14Z', 'name': 'v001', 'size': 
456456}, {'source': None, 'serial': '4364576', 
'created': '2018-11-12T04:27:16Z', 'name': 'v002', 
'size': 345435}, {'source': None, 'serial': 
'6445645', 'created': '2018-11-12T04:27:17Z', 'name': 'v003', 'size': 
23432}, {'source': None, 
'serial': 'we43235', 'created': '2018-11-12T04:27:20Z', 
'name': 'v004', 'size': 35435}]

I'm doing this to get the value of 'name'

 test_url = 'https://0.0.0.0/api/1.1/volume'
 test_data = json.loads(r.get(test_url, headers=headers, 
 verify=False).content.decode('UTF-8'))

 new_data = [{
         'name': value['name']
 } for value in test_data]

 final_data = [val['name'] for val in new_data]

 for k in final_data:
         print(k)

k prints out all the values in name, but i'm stuck at where i want to be able to use it in assigning different API endpoints. Now, k returns

v001
v002
v003
v004

I want to assign each one of them to different endpoints like below:

url_v001 = test_url + v001
url_v002 = test_url + v002
url_v003 = test_url + v003
url_v004 = test_url + v004

I want this to be dynamically done, because there may be more than 4 volume names returned by my main API.

1 Answer 1

2

It wouldn't be good to do that, but the best way is to use a dictionary:

d={}
for k in final_test:
    d['url_'+k] = test_url + k

Or much better in a dictionary comprehension:

d={'url_'+k:test_url + k for k in final_test}

And now:

print(d)

Both reproduce:

{'url_v001': 'https://0.0.0.0/api/1.1/volumev001', 'url_v002': 'https://0.0.0.0/api/1.1/volumev002', 'url_v003': 'https://0.0.0.0/api/1.1/volumev003', 'url_v004': 'https://0.0.0.0/api/1.1/volumev004'}

To use d:

for k,v in d.items():
    print(k+',',v)

Outputs:

url_v001, https://0.0.0.0/api/1.1/volumev001
url_v002, https://0.0.0.0/api/1.1/volumev002
url_v003, https://0.0.0.0/api/1.1/volumev003
url_v004, https://0.0.0.0/api/1.1/volumev004
Sign up to request clarification or add additional context in comments.

7 Comments

I feel like this could easily be simplified into a dictionary comprehension.
@Tomothy32 Edited mine.
@U9-Forward Thanks very much! Appreciate your help. But if its put in a dictionary, how do i use each URL separately? Like, i use each url and loop through them to get my desired values.
@Bharath Edited mine, also see the documentation, docs.python.org/3/tutorial/datastructures.html#dictionaries, happy to help :-)
@U9-Forward thanks for your help. Just one more, when i do print(d) it prints out 4 duplicates (like it prints 4 lines (dictionary) of the same first line).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.