Newbie: last item of a loop

your humble servant stgcri01 at univ-mlv.fr
Fri May 2 09:07:59 EDT 2003


> how do I determin that the current item in a for loop is the last 
> (first) item of a sequence?
> 
> Is there a more efficiant (in terms of cpu-resources) way than:
> 
> for i in my_sequence:
>   if i.index < len(my_sequenec):
>   print "Last item!"

Why not

map(what_you_wish_to_do, yoursequence[:-1])
print '%r: last but not least' %yoursequence[-1]

instead?





More information about the Python-list mailing list