[Tutor] Loop not iterating
Nym City
nymcity at yahoo.com
Tue Jun 30 03:05:13 CEST 2015
Hello all,
Thank you for your time and tips. The reason why I decided to create a loop is because the output is cleaner and did not require any formatting. However, based on what I have learned from all of your responses, that is not going to work with what I am trying to do.
Here is the updated code:
import csvdomains = open('top500domains.csv')domainsReader = csv.reader(domains)domainLists = list(domainsReader)print('\n'.join(["https://www ." + str(lst) for lst in domainLists]), sep='\n')
-------------------------
It is almost how I want it - however, I cannot seem to figure out how to format the output to drop the [' '] from the output.
For example, here is how the output comes up:https://www .['facebook.com/']
I would like to get it in:https://www.facebook.com
Please suggest what is the best, as well as the easiest way to format data out of lists. (I image myself creating a lot of lists and would be helpful to know how to generally handle formatting with lists)
Thanks in advance! :))
Thank you.
On Sunday, June 28, 2015 10:57 PM, Steven D'Aprano <steve at pearwood.info> wrote:
Hi Nym, and welcome,
On Sun, Jun 28, 2015 at 07:32:40PM +0000, Nym City via Tutor wrote:
[...]
> 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.
Of course it does. Each time you go through the loop, you change the
value of "something" to the new value.
If you do this:
x = 23
x = 42
print(x)
What do you expect to print? Hopefully 42.
If you want multiple values, you need something like a list:
x = []
x.append(23)
x.append(42)
print(x)
Of course, in *this* case, there is an easier way to create a list with
two values:
x = [23, 42]
but in the general case where you don't know how many values you will
have, you need to add them using append inside a loop.
--
Steve
_______________________________________________
Tutor maillist - Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list