[Python-ideas] first() and last() tests in for x in y loops
et gmail
eric.twilegar at gmail.com
Tue Jul 20 19:03:07 CEST 2010
While doing "for x in y" loops I often need to know if I'm working on the
first item or the last item in the list.
For example imagine you are building a list of values separated by ","s. The
last iteration you need to suppress the ",". One work around is to just take
the last character off during at the end, but you get the idea.
I could see the code looking something like this
for item in List:
if __first__:
print 'we are in the first loop'
doSomething()
if __last__ is False:
print ','
Sorry if the formatting is a little off.
Does something like this already exist and I'm just being a newb. I am
fairly new to the language.
Also it would be nice if there was an auto counter like
For item in List with count x . where x would then be an auto counter that
incremented every iteration of the loop.
Thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20100720/11ca6bf7/attachment.html>
More information about the Python-ideas
mailing list