[Tutor] Question regarding list editing (in place)

David J. Weller-Fahy dave-lists-tutor at weller-fahy.com
Fri Feb 15 05:25:37 CET 2008


Sorry for the delayed reply - the list software was "helping" me by not 
sending me the list copy.  Heh.

* Kent Johnson <kent37 at tds.net> [2008-02-05 13:43]:
> bob gailer wrote:
>> dirs = [dir for dir in dirs if not dir.startswith(u'.')]
>
> Except to filter the directory list for os.walk() you have to modify the 
> list in place. Use this:
> dirs[:] = [dir for dir in dirs if not dir.startswith(u'.')]

That is exactly what I was missing!  Thanks for the nudge, that works.

... later ...

Of course, if I had paid more attention to section "3.1.4 Lists" in the 
python tutorial, I might have noticed the example which is similar to that 
form:

#v+
>>> # Clear the list: replace all items with an empty list
>>> a[:] = []
>>> a
[]
#v-

Ah, well.  Thanks again for the tips, gentlemen.

Regards,
-- 
David J. Weller-Fahy        | 'These are the questions that kept me out
largely at innocent dot com |  of the really *good* schools.'
dave at weller-fahy dot com |                  - One of The Group


More information about the Tutor mailing list