How do I enter values ​​from a list into a string?

I am trying to enter the following list lines in the url line below. When I do the following:

tickers = ['AAPL','YHOO','TSLA','NVDA'] url = 'http://www.zacks.com/stock/quote/{}'.format(tickers)` 

Python returns

 http://www.zacks.com/stock/quote/['AAPL', 'YHOO', 'TSLA', 'NVDA']` 

Instead, I would like to iterate through the list and return the following:

 http://www.zacks.com/stock/quote/AAPL http://www.zacks.com/stock/quote/YHOO http://www.zacks.com/stock/quote/TSLA http://www.zacks.com/stock/quote/NVDA 

Thanks.

+5
source share
6 answers

Great map trick:

 url = 'http://www.zacks.com/stock/quote/{}' tickers = ['AAPL','YHOO','TSLA','NVDA'] list(map(url.format, tickers)) 

 ['http://www.zacks.com/stock/quote/AAPL', 'http://www.zacks.com/stock/quote/YHOO', 'http://www.zacks.com/stock/quote/TSLA', 'http://www.zacks.com/stock/quote/NVDA'] 
+7
source

Use this:

 tickers = ['AAPL','YHOO','TSLA','NVDA'] url = 'http://www.zacks.com/stock/quote/' ['{}{}'.format(url, x) for x in tickers] 

Result:

 ['http://www.zacks.com/stock/quote/AAPL', 'http://www.zacks.com/stock/quote/YHOO', 'http://www.zacks.com/stock/quote/TSLA', 'http://www.zacks.com/stock/quote/NVDA'] 
+5
source

Just go through tickers with a loop and tickers lines together:

 tickers = ['AAPL','YHOO','TSLA','NVDA'] url = 'http://www.zacks.com/stock/quote/' for ticker in tickers: print(url + ticker) # http://www.zacks.com/stock/quote/AAPL # http://www.zacks.com/stock/quote/YHOO # http://www.zacks.com/stock/quote/TSLA # http://www.zacks.com/stock/quote/NVDA 

Or with a list:

 [url + ticker for ticker in tickers] 

What gives the combined lines in the list:

 ['http://www.zacks.com/stock/quote/AAPL', 'http://www.zacks.com/stock/quote/YHOO', 'http://www.zacks.com/stock/quote/TSLA', 'http://www.zacks.com/stock/quote/NVDA'] 
+2
source

You need to join the list before submitting it. If you are trying to make one url with all parameters in the url, do it first:

 params = "".join(tickers) url = 'http://www.zacks.com/stock/quote/{}'.format(params) 

If you want multiple URLs with one parameter to execute each time:

 urls = [] for param in tickers: urls.append('http://www.zacks.com/stock/quote/{}'.format(param)) 
+2
source

you can try the following:

 tickers = ['AAPL','YHOO','TSLA','NVDA'] url = 'http://www.zacks.com/stock/quote/' for e in tickers: print(url + e) 

this will print the urls, you can also add them to the list instead.

+1
source

Try the following: -

 tickers = ['AAPL','YHOO','TSLA','NVDA'] url = 'http://www.zacks.com/stock/quote/' new_ls = (x+y for x,y in zip([url]*len(tickers),tickers)) for new_url in new_ls: print(new_url) 
0
source

Source: https://habr.com/ru/post/1275379/