What is the best way to delete strings in a string list that that match certain pattern?

Robert P. J. Day rpjday at crashcourse.ca
Sat Nov 7 13:20:47 EST 2009


On Sat, 7 Nov 2009, Peng Yu wrote:

> On Fri, Nov 6, 2009 at 5:57 PM, Dave Angel <davea at ieee.org> wrote:

> > But if you have an expression you want to match each dir against,
> > the list comprehension is the best answer.  And the trick to
> > stuffing that new list into the original list object is to use
> > slicing on the left side.  The [:] notation is a default slice
> > that means the whole list.
> >
> >   dirs[:] = [ item for item in dirs if     bool_expression_on_item ]
>
> I suggest to add this example to the document of os.walk() to make
> other users' life easier.

  huh?  why do you need the slice notation on the left?  why can't you
just assign to "dirs" as opposed to "dirs[:]"?  using the former seems
to work just fine.  is this some kind of python optimization or idiom?

rday
--


========================================================================
Robert P. J. Day                               Waterloo, Ontario, CANADA

            Linux Consulting, Training and Kernel Pedantry.

Web page:                                          http://crashcourse.ca
Twitter:                                       http://twitter.com/rpjday
========================================================================


More information about the Python-list mailing list