How to detect the last element in a for loop

Christopher A. Craig list-python at ccraig.org
Mon Jul 29 00:41:10 EDT 2002


Tom Verbeure <tom.verbeure at verizon.no.sp.am.net> writes:

> Then what about this?
> 
> for a in myList:
>     if last_one:
>         special_stuff
>     do_stuff
> Tom

Okay, not as simple, but it should work

for a, i in zip(myList, xrange(len(myList))):
     if i==(len(myList)-1):
          special_stuff
     do_stuff

-- 
Christopher A. Craig <list-python at ccraig.org>
"Imagination is more important than knowledge" Albert Einstein




More information about the Python-list mailing list