[Tutor] lists
Raymond Hettinger
python@rcn.com
Tue, 26 Mar 2002 07:17:07 -0500
Hello Ian,
The plus operator for lists concatenates two sequences.
'Word' is interpreted as a sequence of letters in your example.
Putting brackets around it, makes the word itself a sequence of one.
Alternatively, the usual style for building a list is to use the append
method.
Replace 'fillme += word'
with 'fillme += [word]'
or with 'fillme.append(word)'
Raymond Hettinger
----- Original Message -----
From: "Ian!" <imcmeans@shaw.ca>
To: <Tutor@python.org>
Sent: Tuesday, March 26, 2002 2:34 AM
Subject: Re: [Tutor] lists
> Hello! I'd just like to say that I've been happily using python on XP for
a
> few months now. (to answer Rob's question). I haven't run into any
glitches.
>
> Okay, here's a question. How should I access the elements of a list in a
for
> loop? Right now, when I try to do it, I get a list that contains all of
the
> letters of each element in the list. Here's what I mean:
>
> >>> somewords = ['goo', 'foo', 'loo', 'moo', 'too']
> >>> fillme = []
> >>> for word in somewords:
> fillme += word
> >>> fillme
> ['g', 'o', 'o', 'f', 'o', 'o', 'l', 'o', 'o', 'm', 'o', 'o', 't', 'o',
'o']
>
> How can I make the for loop work with whole strings at a time, instead of
> characters?
>
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>