Newbie: last item of a loop

Anton Muhin antonmuhin at sendmail.ru
Fri May 2 08:11:18 EDT 2003


Sven Brandt wrote:
> Hi,
> 
> to those familiar with Zope:
> Is there an equivalent the the tag variable of dtml-in 'sequence-end'?
> 
> In "python-speak":
> 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!"
> 
> Regards
> Sven
> 
  Unfortunatley, you underdescribe your problem, but this might be what 
you want:

l = ['a', 'b', 'c', 'd']

# process l[0]
for e in l[1:-1]:
     # process inner elements
# process l[-1]

hth,
Anton





More information about the Python-list mailing list