[Tutor] Very basic question about lists
Eduardo Vieira
eduardo.susan at gmail.com
Mon Dec 22 22:27:02 CET 2008
Wesley wrote:
On Mon, Dec 22, 2008 at 1:19 PM, wesley chun <wescpy at gmail.com> wrote:
>
> in addition, i think (and i may be wrong about this) that he really
> wanted to do:
>
> if 'arr' in list1 or 'bell' in list1...
>
Thanks for all the replies. Yes that was actually what I meant. My
mistake too was that I gave you all a wrong example to explain my
problem, sorry. The following example will explain better:
list1 = ['ar', 'fir', 'wo']
list2 = ['ber', 'gar', 'gt']
list3 = ['hu', 'mo', 'ko', 'tr']
list4 = ['q', 'wer', 'duh']
whole = [list1, list2, list3, list4]
for item in whole:
if 'ar' or 'ko' in item:
print item
So, the unexpected result was that I got all lists printed, when I
expected only list1 and list3. Now, I don't know if I still understand
the explanation given by
spir -- I'll reread more carefully--, but I know that the code to get
what I expect is this instead:
for item in whole:
for word in item:
if word == 'ar' or word == 'ko':
print item
I see I have to do a loop inside a loop and that this the right expression
if word == 'ar' or word == 'ko':
but this is not:
if word == 'ar' or 'ko':
More information about the Tutor
mailing list