For loops with explicit indices -- again

Jim Richardson warlock at eskimo.com
Wed Mar 20 03:23:52 EST 2002


On Tue, 19 Mar 2002 18:57:29 +0100,
 Armin Rigo <arigo at ulb.ac.be> wrote:
> Hello everybody,
> 
> Here is yet another proposal (or did I miss something similar?) about the
> for-with-explicit-indices syntax problems, as discussed in PEP 281
> (http://python.sourceforge.net/peps/pep-0281.html).
> 
> Now that Python has iterators, they could be more explicitely used by user
> code. Remember that 'for x in sequence' is equivalent to 'for x in
> iter(sequence)', where iter() is used to get a sequence iterator. As it
> seems we often loop over sequence elements and occasionnally need an
> explicit index, I suggest adding a 'count' attribute to sequence iterators
> that returns the number of items returned so far. This just requires putting
> the iterator in a local variable. For example, to decrement all positive
> integers in a list, modifying it in-place:
> 
>     it = iter(lst)
>     for x in it:
>         if x>0:
>             lst[it.count-1] -= 1


I am missing something. How is this different from 

for x in len(lst):
    

?
 


-- 
Jim Richardson
	Anarchist, pagan and proud of it
www.eskimo.com/~warlock
    Linux, for when you need to get work done, Or you could just play UT...



More information about the Python-list mailing list