How to write a generator that returns ALL-BUT-LAST items in the iterable in Python?
<p>I asked some similar questions [1, 2] yesterday and got great answers, but I am not yet technically skilled enough to write a generator of such sophistication myself.</p>

<p>How could I write a generator that would raise StopIteration if it's the last item, instead of yielding it?</p>

<p>I am thinking I should somehow ask two values at a time, and see if the 2nd value is StopIteration. If it is, then instead of yielding the first value, I should raise this StopIteration. But somehow I should also remember the 2nd value that I asked if it wasn't StopIteration.</p>

<p>I don't know how to write it myself. Please help.</p>

<p>For example, if the iterable is [1, 2, 3], then the generator should return 1 and 2.</p>

<p>Thanks, Boda Cydo.</p>

<p>[1] <a href="http://stackoverflow.com/questions/2421355/how-do-i-modify-a-generator-in-python">http://stackoverflow.com/questions/2421355/how-do-i-modify-a-generator-in-python</a></p>

<p>[2] <a href="http://stackoverflow.com/questions/2421368/how-to-determine-if-the-value-is-one-but-last-in-a-python-generator">http://stackoverflow.com/questions/2421368/how-to-determine-if-the-value-is-one-but-last-in-a-python-generator</a></p>
