Reputation: 165
I am trying to build a dynamic URL with some of the information stored in a list. I have loaded the list with some values and then iterate through the list concatenating the value from the list with a prefix. I then want to reference that concatenated value which matches a preloaded variable.
In the below code url_var just returns the name of the variable but not the value of the variable.
base_url_asia = "https://www.location1.com/"
base_url_americas = "https://www.location2.com/"
regions = [asia, americas]
for i in range(len(regions)):
url_var = 'base_url_' + regions[i]
print(url_var)
I expect the output to be the full URL however all I get is base_url_asia or base_url_americas and not the actual url.
Upvotes: 0
Views: 58
Reputation: 66
You are defining variables that you are not using. 'base_url_' is a string and not a variable. If you want to store different locations using the same variable but with different names, you should use a dictionary.
base_url=dict()
base_url['asia'] = 'www.location1.com'
base_url['americas'] = 'www.location2.com'
continent = ['asia','americas']
for cont in continent:
print(base_url[cont])
Note that cont is not an integer, but is the name of the continents.
I hope you find it useful. Good luck!
Upvotes: 1