> *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'
There are several ways to do this but the two that I would suggest are: for num in x: if 5 in num: found = True break if found: print 'yes' Or using list comprehensions(Which you probably haven't discovered yet) resp = ['yes' for num in x if 5 in num] if resp: print resp[0] HTH Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor