[CentralOH] Detect last iteration of iterator
mark at microenh.com
Mon May 9 17:54:59 CEST 2011
Given a for loop that iterates through an iterable (for i in x:), is there a way to detect when the code is in the last iteration, so that it can do something slightly different?
For a contrived example, assume I want to duplicate the string join method to insert a string between, but not following, items in a list.
The only way I can think if is to split the iteration into:
for i in x[:-1]
<do something with i>
<do something slightly different with x[-1]>
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the CentralOH