Reputation: 19
I'm working with APIs and I need to build a URL to retrieve the information I'm asked. The parameter of the function is a list with the codes of different countries. The basic format of the URL is https://restcountries.com/v2/alpha?codes={code},{code},{code} with each code coming from the parameter list. However, the number of items in the list can vary, so how can I build the url with the appropriate number of codes?
For example: if the list is ["kor", "usa", "de"] the url should look like https://restcountries.com/v2/alpha?codes=kor,usa,de
Upvotes: 0
Views: 115
Reputation: 1069
def createUrl(codes):
url = " https://restcountries.com/v2/alpha?codes="
for i, code in enumerate(codes):
url += code
if i != len(codes)-1: url += ","
return url
For the input ['foo','bar','baz']
, this code will produce https://restcountries.com/v2/alpha?codes=foo,bar,baz
Upvotes: 1