Alternative to standard C "for"

John Machin sjmachin at lexicon.net
Thu Feb 17 15:00:29 EST 2005


James Stroud wrote:
>
> It seems I need constructs like this all of the time
>
> i = 0
> while i < len(somelist):
>   if oughta_pop_it(somelist[i]):
>     somelist.pop(i)
>   else:
>     i += 1
>
> There has to be a better way...
>

! for i in xrange(len(somelist)-1, -1, -1):
!     if oughta_pop_it(somelist[i]):
!         del somelist[i] # why use pop??

We don't need no stinking iterators; we're the
Python_1.5_or_earlier_istas!




More information about the Python-list mailing list