CodingInCircles
CodingInCircles

Reputation: 2807

Python List Comprehensions - Join with For loop

I am trying to generate URLs as follows:

http://ergast.com/api/f1/2000/qualifying?limit=10000

I am using Python to generate URLs for the years 2000 to 2015, and to that end, wrote this code snippet:

url = "http://ergast.com/api/f1/"
year = url.join([str(i) + "/qualifying?limit=10000" + "\n" for i in range(1999, 2016)])
print(year)

The output is:

1999/qualifying?limit=10000
http://ergast.com/api/f1/2000/qualifying?limit=10000
http://ergast.com/api/f1/2001/qualifying?limit=10000
http://ergast.com/api/f1/2002/qualifying?limit=10000
http://ergast.com/api/f1/2003/qualifying?limit=10000
http://ergast.com/api/f1/2004/qualifying?limit=10000
......
http://ergast.com/api/f1/2012/qualifying?limit=10000
http://ergast.com/api/f1/2013/qualifying?limit=10000
http://ergast.com/api/f1/2014/qualifying?limit=10000
http://ergast.com/api/f1/2015/qualifying?limit=10000

How do I get rid of the first line? I tried making the range (2000, 2016), but the same thing happened with the first line being 2000 instead of 1999. What am I doing wrong? How can I fix this?

Upvotes: 3

Views: 18165

Answers (4)

C Panda
C Panda

Reputation: 3405

You could use the cleaner and more powerful string formatting as follows,

fmt = "http://ergast.com/api/f1/{y}/qualifying?limit=10000"
urls = [fmt.format(y=y) for y in range(2000, 1016)]

In your code the use of str.join is questionable as it has a semantics different from what you are trying to accomplish. s.join(ls), joins the items of list ls by str s. If ls = [l1, l2 ,...] , it returns str(l1) + s + str(l2) + s..

Upvotes: 2

AKS
AKS

Reputation: 19811

You can use string formatting for this:

url = 'http://ergast.com/api/f1/{0}/qualifying?limit=10000'

print('\n'.join(url.format(year) for year in range(2000, 2016)))

# http://ergast.com/api/f1/2000/qualifying?limit=10000
# http://ergast.com/api/f1/2001/qualifying?limit=10000
# ...
# http://ergast.com/api/f1/2015/qualifying?limit=10000

UPDATE:

Based on OP's comments to pass these urls in requests.get:

url_tpl = 'http://ergast.com/api/f1/{0}/qualifying?limit=10000'

# use list coprehension to get all the urls
all_urls = [url_tpl.format(year) for year in range(2000, 2016)]

for url in all_urls:
    response = requests.get(url)

Upvotes: 9

dfranca
dfranca

Reputation: 5322

It's good to understand why it's happening. For that you need to understand the join function, look the docs

Concatenate a list or tuple of words with intervening occurrences of sep.

That means that your url parameter will be repeated in between the words you want to concatenate, what will result in the output above, with the first element without the url. What you want is not use join, is to concatenate the strings as you're already doing with the year.

For that you can use different methods, as was already answered. You can use string formatting as was pointed out by @AKS and it should work.

Upvotes: 1

tobias_k
tobias_k

Reputation: 82899

Instead of using the URL to join the string, use a list comprehension to create the different URLs.

>>> ["http://ergast.com/api/f1/%d/qualifying?limit=10000" % i for i in range(1999, 2016)]
['http://ergast.com/api/f1/1999/qualifying?limit=10000',
 'http://ergast.com/api/f1/2000/qualifying?limit=10000',
 ...
 'http://ergast.com/api/f1/2014/qualifying?limit=10000',
 'http://ergast.com/api/f1/2015/qualifying?limit=10000']

You could then still use '\n'.join(...) to join all those to one big string, it you like.

Upvotes: 4

Related Questions