How to detect the last element in a for loop
Andreas Kostyrka
andreas at kostyrka.priv.at
Tue Jul 30 16:52:01 EDT 2002
Am Son, 2002-07-28 um 15.16 schrieb Tom Verbeure:
>
>
> > For a simple solution, how about:
> >
> > for a in myList[:-1]:
> > do_stuff(a)
> > special_stuff(myList[-1])
>
> No, I still want to do 'do_stuff' for the last element also. This may be,
> say, 10 lines of code. Too much to duplicate it outside the loop, not
> enough for a separate function...
Then do this:
for a in myList:
do_stuff(a):
if myList:
special_stuff(myList[-1])
Andreas
More information about the Python-list
mailing list