Is this a safe thing to do?

Mel Wilson mwilson at the-wire.com
Thu Oct 9 17:34:56 EDT 2003


In article <5rycnTJjK94NEBiiXTWJhg at comcast.com>,
"Terry Reedy" <tjreedy at udel.edu> wrote:
>
>"Duncan Booth" <duncan at NOSPAMrcp.co.uk> wrote in message
>news:Xns940F8C176F22Bduncanrcpcouk at 127.0.0.1...
>> The answer is to iterate over a COPY of the list if you want to
>modify it
>> during the iteration:
>
>or, when deleting items, carefully iterate backwards
>
>x=[{'f':9},{'f':1},{1:3,'f':9},{'f':9},{'f':3}]
>for i in range(len(x)-1,-1,-1):
>    if x[i]['f'] == 9:
>        del x[i]
>
>>>> x
>[{'f': 1}, {'f': 3}]

   Apropos of something else, I'm getting really sympathetic
with the plight of the reverse-iterator proponents.  Until
they get their way,

        def xreverse (start, stop, split):
            return xrange (stop-split, start-split, -split)



        Regards.        Mel.




More information about the Python-list mailing list