<div dir="ltr">There are several different blocks of code you could tack onto a loop (I've deliberately chosen somewhat unusual words to express these here):<br><br> for x in items:<br> # body<br> interstitially:<br>
# things to do between loop iteration<br> # (executed after each iteration in the loop when there is a next value)<br> subsequently:<br> # things to do after the last element of the loop is processed<br>
# (when the loop is not exited by break)<br> contrariwise:<br> # things to do if the list was empty<br><br>For example:<br><br> result = ""<br> for x in items:<br> result += str(x)<br>
interstitially:<br> result += ", "<br> contrariwise:<br> result = "no data"<br><br>When I first learned that Python had an 'else' clause on loops, I assumed it meant 'contrariwise'. I was surprised that it actually meant 'subsequently'. <br>
<br>To be more clear, contrariwise is essentially equivalent to:<br><br> empty = True<br> for x in items:<br> empty = False<br> # body<br> if empty:<br> # do contrariwise code<br><br>and interstitially is essentially equivalent to:<br>
<br> first = True<br> for x2 in items:<br> if not first:<br> # do interstitial code<br> first = False<br> x = x2<br> # body<br><br>I think these are common/useful paradigms. I'm curious what others think.<br>
<br>--- Bruce<br><br></div>