[Tutor] 'for' iteration stop problem

Wayne Werner waynejwerner at gmail.com
Sun Apr 3 01:38:12 CEST 2011


On Sat, Apr 2, 2011 at 5:45 PM, Alan Gauld <alan.gauld at btinternet.com>wrote:

>
> There are other ways but those should cover most eventualities.


I find that my preferred way when the loops are nested is to move the loops
into a function and then return:

def do_something(collection, collection2):
    for x in  collection:
        for y in collection 2:
             if someCase(x,y):
                 return

It seems (at least to me) a little nicer than sentinel values or throwing an
exception.

Just my $.02
-Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110402/edae52e9/attachment.html>


More information about the Tutor mailing list