[Python-Dev] a feature i'd like to see in python #1: betteriteration control (original) (raw)
Terry Reedy tjreedy at udel.edu
Sun Dec 3 20:52:24 CET 2006
- Previous message: [Python-Dev] a feature i'd like to see in python #1: betteriteration control
- Next message: [Python-Dev] [NPERS] Re: a feature i'd like to see in python #1: betteriteration control
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
"Ben Wing" <ben at 666.com> wrote in message news:4572C1F1.1050600 at 666.com...
many times writing somewhat complex loops over lists i've found the need to sometimes delete an item from the list. currently there's no easy way to do so; basically, you have to write something like
i = 0 while i < len(list): el = list[i] ...do something... if el should be deleted: del list[i] else: i += 1
My view: while loops are the standard construct for iteration. They give you complete control over the control variable(s). Hence no need for 'better iteration control'. The above is easy enough and quite clear.
note that you can't write
for x in list: or even for i in xrange(len(list)):
For loops are for the common case of straightforwardly iterating over a sequence. They are efficient both to write and execute. Let's not mess with them.
-1
note also that you need to do some trickiness to adjust the index appropriately when deleting.
Iterate in reverse and no index adjustment is needed
Terry Jan Reedy
- Previous message: [Python-Dev] a feature i'd like to see in python #1: betteriteration control
- Next message: [Python-Dev] [NPERS] Re: a feature i'd like to see in python #1: betteriteration control
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]