Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #100199

Re: filter a list of strings

From Sivan Greenberg <sivan@vitakka.co>
Newsgroups comp.lang.python
Subject Re: filter a list of strings
Date 2015-12-09 16:35 +0200
Message-ID <mailman.94.1449671840.12405.python-list@python.org> (permalink)
References <3p9zlt5t5Vz5vN5@dovecot03.posteo.de> <mailman.171.1449145727.14615.python-list@python.org> <2598115.t018ozAsXX@PointedEars.de>

Show all headers | View raw


That might also work:

new_list = [i for i in the_list if i not in targets]
# given you have no special requirements for the selection
# out of 'targets'

-Sivan

On Wed, Dec 9, 2015 at 12:58 AM, Thomas 'PointedEars' Lahn <
PointedEars@web.de> wrote:

> Mark Lawrence wrote:
>
> > On 03/12/2015 01:15, c.buhtz@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.
> --
> https://mail.python.org/mailman/listinfo/python-list
>



-- 
Sivan Greenberg
Co founder & CTO
Vitakka Consulting

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Re: filter a list of strings Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-12-03 12:28 +0000
  Re: filter a list of strings Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-12-08 23:58 +0100
    Shadowing built-ins [was Re: filter a list of strings] Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-12-09 18:38 +1100
    Re: filter a list of strings Sivan Greenberg <sivan@vitakka.co> - 2015-12-09 16:35 +0200

csiph-web