filter a list of strings
Mark Lawrence
breamoreboy at yahoo.co.uk
Thu Dec 3 07:28:07 EST 2015
On 03/12/2015 01:15, c.buhtz at posteo.jp wrote:
> I would like to know how this could be done more elegant/pythonic.
>
> I have a big list (over 10.000 items) with strings (each 100 to 300
> chars long) and want to filter them.
>
> list = .....
>
> for item in list[:]:
> if 'Banana' in item:
> list.remove(item)
> if 'Car' in item:
> list.remove(item)
>
> There are a lot of more conditions of course. This is just example code.
> It doesn't look nice to me. To much redundance.
targets = ['Banana', 'Car'...]
for item in list[:]:
for target in targets:
if target in item:
list.remove(item)
>
> btw: Is it correct to iterate over a copy (list[:]) of that string list
> and not the original one?
>
Absolutely :)
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
More information about the Python-list
mailing list