[Tutor] Loop not iterating

Mark Lawrence breamoreboy at yahoo.co.uk
Mon Jun 29 02:06:57 CEST 2015


On 28/06/2015 20:32, Nym City via Tutor wrote:
> Hello,
> I am working on my second program and its a two part progam that I want to design However, i am not sure what silly mistake I am making on the first part:
> Here is my code:
>
> | 2
> 3
> 4
> 5
> 6
> 7 | import csv
> domains = open('top500domains.csv')
> domainsReader = csv.reader(domains)
> domainLists = list(domainsReader)
> for domain in domainLists:
>       something = ("www." + str(domain))
> print(something)
>
>   |
>
> My program reads in a CSV file that has 500 list of domains. However, when I save the output of my loop to the variable name "something" - and later print "something" it contains only 1 entry from my csv file.
> On the other hand, instead of saving of loop output to the variable "something" - if I just print, I get all 500 entries displayed.
> Please advise. Thanks!
>

You are not saving your loop output to anything.  The above code simply 
sets 'something' every time around the loop, and only prints 'something' 
once when the loop has finished.  Try something like this.

urls = [] # a Python list in case you didn't know.
for domain in domainLists:
     urls.append("www." + str(domain)) # add to the end of the list

for url in urls:
     doWhatever(url)

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence



More information about the Tutor mailing list