filter a list of strings

Thomas 'PointedEars' Lahn PointedEars at web.de
Tue Dec 8 17:58:42 EST 2015


Mark Lawrence wrote:

> 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 = .....
>> […]
> 
> 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 :)

However, “list” is a built-in class/constructor that would be overwritten 
this way.  One should choose another identifier than “list” for one’s 
variables.

-- 
PointedEars

Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.



More information about the Python-list mailing list