[CentralOH] Detect last iteration of iterator

Mark Erbaugh 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...
URL: <http://mail.python.org/mailman/private/centraloh/attachments/20110509/8779c618/attachment.html>

More information about the CentralOH mailing list