Last iteration?

Paul McGuire ptmcg at austin.rr.com
Sun Oct 14 19:08:55 CEST 2007


On Oct 14, 5:58 am, Paul Hankin <paul.han... at gmail.com> wrote:
> On Oct 14, 8:00 am, Paul McGuire <pt... at austin.rr.com> wrote:
> > def signal_last(lst):
> >     last2 = None
> >     it = iter(lst)
> >     try:
> >         last = it.next()
> >     except StopIteration:
> >         last = None
> >     for last2 in it:
> >         yield False, last
> >         last = last2
> >     yield True, last
>
> This yields a value when the iterator is empty, which Diez's solution
> didn't. Logically, there is no 'last' element in an empty sequence,
> and it's obscure to add one. Peter Otten's improvement to Diez's code
> looks the best to me: simple, readable and correct.
>

Of course!  For some reason I thought I was improving Peter Otten's
version, but when I modified my submission to behave as you stated, I
ended right back with what Peter had submitted.  Agreed - nice and
neat!

-- Paul





More information about the Python-list mailing list