I am a newbie. I am trying to generate a list of links from words inputted in Python. But it always produces only the last link although I input more than one.
I have tried to look for the answers but just cannot seem to find the right one. Maybe I am still too inexperienced to understand and apply them to my case. Anyway, how can I fix this?
My code:
search = input()
Split = search.split(' ')
print(Split)
for word in Split:
URL = 'https://samplenotspamdotcom/' word
print(URL)
URL_List = URL.split(' ')
print(URL_List)
My output:
['https://samplenotspamdotcom/not']
Expected output:
['https://samplenotspamdotcom/why', 'https://samplenotspamdotcom/not']
CodePudding user response:
You have two options to achieve that first option is to concatenate links with a space in between each link and then split them on space or create a blank list and in loop add them to the list.
Option 1:
URL = ''
for word in Split:
URL = 'https://samplenotspamdotcom/' word ' '
print(URL)
URL.strip() #to remove any extra space on the end
URL_List = URL.split(' ')
print(URL_List)
Option2
URLS = []
for word in Split:
URLS.append('https://samplenotspamdotcom/' word)
print(URLS)