I'm looking for a pythonic way to iterate through a list and do something on the last (and only the last) element. There are two ways I can see to do this, of which I would guess the second is best:
for item in a_list:
#do something to every element
if a_list.index(item) == len(a_list) - 1:
# do something to the last one
and
for n, item in enumerate(a_list):
#do something to every element
if n == len(a_list) - 1 :
# do something to the last one
However, I wonder if there is a way of doing it without calling len()
on a list I'm already iterating over. I'm quite happy, by the way, to be told that this isn't something I should worry about.