for: else: - any practical uses for the else clause?

Amaury Forgeot d'Arc afaNOSPAM at neuf.fr
Tue Sep 26 17:15:27 EDT 2006


metaperl.etc at gmail.com a écrit :
> A very old thread:
> http://groups.google.com/group/comp.lang.python/browse_frm/thread/2c5022e2b7f05525/1542d2041257c47e?lnk=gst&q=for+else&rnum=9#1542d2041257c47e
> 
> discusses the optional "else:" clause of the for statement.
> 
> I'm wondering if anyone has ever found a practical use for the else
> branch?
> 

I use it regularly in contructs like:

     for value in someList:
         if someCondition(value):
             print "a matching item was found"
             break
     else:
         print "no matching item"
         return False

     # ... continue with value

-- 
Amaury



More information about the Python-list mailing list