Python join in 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?

asked Apr 13, 2016 at 9:09

CodingInCirclesCodingInCircles

2,38510 gold badges57 silver badges84 bronze badges

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)

answered Apr 13, 2016 at 9:14

Python join in for loop

1

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.

answered Apr 13, 2016 at 9:14

tobias_ktobias_k

79.4k11 gold badges114 silver badges171 bronze badges

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..

answered Apr 13, 2016 at 12:55

C PandaC Panda

3,1072 gold badges10 silver badges11 bronze badges

2

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.

answered Apr 13, 2016 at 9:19

Python join in for loop

dfrancadfranca

4,9192 gold badges30 silver badges57 bronze badges

How do you join a loop in python?

Python join string and int Here, we used the map function along with string join to achieve the same result. Refer to the map function. It is another approach to concatenating string and int using the join method. Here, we are using the format function inside the for loop to format list items.

How do I concatenate a string in a for loop?

This rule finds code that performs string concatenation in a loop using the + operator. If the enclosing loop in question is executed many times, the use of the + operator may be inefficient.

Can we join list in python?

Python Join List. We can use the python string join() function for joining a list of the strings. However, this function takes iterable as an argument and the list is iterable, so we are able to use it with the list.

How do you join a list in python?

One of the easiest ways are by using the + operator..
Join two list: list1 = ["a", "b", "c"] list2 = [1, 2, 3] list3 = list1 + list2. ... .
Append list2 into list1: list1 = ["a", "b" , "c"] list2 = [1, 2, 3] for x in list2: ... .
Use the extend() method to add list2 at the end of list1: list1 = ["a", "b" , "c"] list2 = [1, 2, 3].