Alternative to standard C "for"

Kent Johnson kent37 at tds.net
Thu Feb 17 14:06:06 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...

somelist[:] = [ item for item in somelist if not oughta_pop_it(item) ]

Kent



More information about the Python-list mailing list