[Tutor] for loop

Ching-Yi Chan m92507 at stumail.nutn.edu.tw
Tue Apr 19 05:51:39 CEST 2005


*Ron A*  /Wed Jan  7 18:41:15 EST 2004/

I'm experimenting and would like 'yes' to be printed only if 5 is not in
the list, but I want to look in each list. This prints out two yeses.
How do I get it to print just one 'yes'?

x = [[1,2,3],[2,4,6],[8,4,5,6],[9,8,7]]

for num in x:
    if 5 in num:
        break
    else:
        print 'yes'  

--------------------------------------------------------------------------

Hi, I read the code and consider for a while, you can try it :

x = [[1,2,3],[2,4,6],[8,4,5,6],[9,8,7]]
print [ e for e in x if 5 in e]




More information about the Tutor mailing list