remove a list from a list

Neil Cerutti horpner at yahoo.com
Fri Nov 17 16:03:43 EST 2006


On 2006-11-17, Rares Vernica <rvernica at gmail.com> wrote:
> Sorry for not being clear from the beginning and for not using
> clear variable names.
>
> Problem context:
>
> import os
> dirs_exclude = set(('a', 'b', 'e'))
> for root, dirs, files in os.walk('python/Lib/email'):
>      # Task:
>      # delete from "dirs" the directory names from "dirs_exclude"
>      # case-insensitive
>
> The solution so far is:
>
> for i in xrange(len(dirs), 0, -1):
>    if dirs[i-1].lower() in dirs_exclude:
>      del dirs[i-1]
>
> I am looking for a nicer solution.

I'd probably just skip over those dirs as I came them instead of
troubling about mutating the list. Unless the list is needed in
more than one place.

-- 
Neil Cerutti



More information about the Python-list mailing list