Re: [Python-ideas] first() and last() tests in for x in y loops

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

On Tue, Jul 20, 2010 at 1:03 PM, et gmail <eric.twilegar@gmail.com> wrote: ..
This feature is already there: for num,item in enumerate(List): ... I would like to suggest that when you don't know how to achieve certain result in python, you first ask on python-list or #python IRC channel. It is best to propose new features after users agree that the feature is not present.

On Tue, Jul 20, 2010 at 12:03:07PM -0500, et gmail wrote:
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.
See http://ppa.cvs.sourceforge.net/ppa/misc/Repeat.py?rev=HEAD&content-type=text/vnd.viewcvs-markup (License: Python)
Also it would be nice if there was an auto counter like
Find built-in function enumerate() in the docs. Oleg. -- Oleg Broytman http://phd.pp.ru/ phd@phd.pp.ru Programmers don't die, they just GOSUB without RETURN.

On 7/20/2010 1:03 PM, et gmail wrote:
Do not use tabs when posting code.
Does something like this already exist
I am posting an extended answer to "How to treat the first or last item differently" on python-list (gmane.comp.python.general) so that others can see and find it. -- Terry Jan Reedy

On Tue, Jul 20, 2010 at 1:03 PM, et gmail <eric.twilegar@gmail.com> wrote: ..
This feature is already there: for num,item in enumerate(List): ... I would like to suggest that when you don't know how to achieve certain result in python, you first ask on python-list or #python IRC channel. It is best to propose new features after users agree that the feature is not present.

On Tue, Jul 20, 2010 at 12:03:07PM -0500, et gmail wrote:
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.
See http://ppa.cvs.sourceforge.net/ppa/misc/Repeat.py?rev=HEAD&content-type=text/vnd.viewcvs-markup (License: Python)
Also it would be nice if there was an auto counter like
Find built-in function enumerate() in the docs. Oleg. -- Oleg Broytman http://phd.pp.ru/ phd@phd.pp.ru Programmers don't die, they just GOSUB without RETURN.

On 7/20/2010 1:03 PM, et gmail wrote:
Do not use tabs when posting code.
Does something like this already exist
I am posting an extended answer to "How to treat the first or last item differently" on python-list (gmane.comp.python.general) so that others can see and find it. -- Terry Jan Reedy
participants (4)
-
Alexander Belopolsky
-
et gmail
-
Oleg Broytman
-
Terry Reedy