iterating through lists to delete elements

Duncan Booth duncan at NOSPAMrcp.co.uk
Fri Aug 10 10:26:49 CEST 2001


"gzeljko" <gzeljko at sezampro.yu> wrote in
news:mailman.997374194.8782.python-list at python.org: 

> From: Mark Robinson <m.1.robinson at herts.ac.uk>
> Subject: iterating through lists to delete elements
> 
>> Can anyone advise me the best way to iterate through a list and
>> deleting elements that don't meet a certain criteria. 
> 
> some_list = [ x for x in some_list if some_test(x) ]
> 
Possibly better to do:
 some_list[:] = [ x for x in some_list if some_test(x) ]

Otherwise any other references to the list won't be updated.

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?



More information about the Python-list mailing list