How to detect the last element in a for loop
François Pinard
pinard at iro.umontreal.ca
Sun Jul 28 10:11:18 EDT 2002
[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...
I did not follow this thread, I may be missing something, but why not:
for a in myList:
do_stuff(a)
special_stuff(a)
taking advantage on the fact that `a' keeps the last value it received?
--
François Pinard http://www.iro.umontreal.ca/~pinard
More information about the Python-list
mailing list