Quote:
|
Originally Posted by zem52887
edit: i'm not sure if this makes sense - does the first for loop create the variable industry_url and store a value into it to be passed to get_company_index?
|
Essentially, yes. Take the following example code:
numbers = [1, 2, 3]
for n in numbers
print 2 * n
The above code is equivalent to the following:
numbers = [1, 2, 3]
n = numbers[0]
print 2 * n
n = numbers[1]
print 2 * n
n = numbers[2]
print 2 * n
I'll also give you a quick runthrough of list comprehensions:
Let's say you have a number, say 5, and you want to double it. You could do something like this:
That's fair enough, but what if you wanted to double every number in a list? That's where list comprehensions come in:
list = [1, 2, 3, 4, 5]
doubled = [x * 2 for x in list]
# doubled equals [2, 4, 6, 8, 10]
With a list comprehension, you can do the same thing to each item in a list. It's a lot like a for-loop, indeed, the two are very much related. You can use a for loop to do the same thing as a list comprehension:
list = [1, 2, 3, 4, 5]
doubled = [] # start with a blank list
for x in list:
doubled.append(x * 2) # add x * 2 onto the new "doubled" list
List comprehensions are just a shortcut.
The key thing is that they only apply to lists. With get_industry_urls, a list of urls is returned: one for each industry. With get_company_index, only one url is returned: the url of the company index for a particular industry.