Home > Blockchain >  How can I join a base URL to partial URLs from a list using For Loops
How can I join a base URL to partial URLs from a list using For Loops

Time:09-16

I have a list called URL_list. I looks like this: [item-1.html', 'item-2.html', 'item-3.html']

I want use a for loop to join the base URL "http://www.examplesite.com/" to each partial URL in my list.

Here's what I tried, but it's not working:

full_URL = [ ]

for base in URL_list:

URL  = "http://www.examplesite.com/"

print(full_URL)  

What should I do to attach the beginning of the URL, "http://www.examplesite.com/", with each item in my list to get a list of the full URLs?

CodePudding user response:

You can either concatenate the string or use f-string for each of the values in the list together with the base URL, you can accomplish it using List Comprehension:

>>> parts = ['item-1.html', 'item-2.html', 'item-3.html']
>>> [f"http://www.examplesite.com/{v}" for v in parts]

['http://www.examplesite.com/item-1.html', 'http://www.examplesite.com/item-2.html', 'http://www.examplesite.com/item-3.html']

CodePudding user response:

It's the same as the above one:

page = ['item-1.html', 'item-2.html', 'item-3.html']
fullUrl=[]
for base in range(len(page)):
    fullUrl.append("http://www.examplesite.com/" page[base])
    print(fullUrl[base])

#To print all links as list
print(fullUrl)
  • Related