<span class="gmail_quote">On 29 Sep 2006 11:26:10 -0700, <b class="gmail_sendername">Klaas</b> <<a href="mailto:mike.klaas@gmail.com">mike.klaas@gmail.com</a>> wrote:</span><br>
<div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">else: does not trigger when there is no data on which to iterate, but<br>when the loop terminated normally (ie., wasn't break-ed out). It is
<br>meaningless without break.</blockquote><div>
<div>The else clause *is* executed when there is no data on which to iterate.<br>
Your example even demonstrates that clearly: <br>
<br>
</div>
</div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">>>> for x in []:<br>... print 'nothing'<br>... else:<br>... print 'done'
<br>...<br>done</blockquote><div><br>
</div>The else clause is executed because the loop still terminates normally with an empty list - albeit without having looped at all.<br>
</div><br>
I agree that it is meaningless without a break statement, but I still
find it useful when I want to determine whether I looped over the whole
list or not. For example, if I want to see whether or not a list
contains an odd number:<br>
<br>
<span style="font-family: courier new,monospace;">for i in list:</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> if i % 2 == 1:</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> print "Found an odd number."</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> break</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">else:</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> print "No odd number found."</span><br style="font-family: courier new,monospace;">
<br>
Without the else clause I would need to use an extra variable as a "flag" and check its value outside the loop:<br>
<br>
<span style="font-family: courier new,monospace;">found = False</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">for i in list:</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> if i % 2 == 1:</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> print "Found an odd number."</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> found = True</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> break</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">if not found:</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> print "No odd number found."</span><br style="font-family: courier new,monospace;">
<br>
OK, so using "else" only saves me 2 lines and a variable - not much to write home about, but I still like it.<br>
<br>
Johan.<br>
<br>