Remove items from a list

Paul McGuire ptmcg at austin.rr._bogus_.com
Wed Sep 8 01:15:08 EDT 2004


"Stan Cook" <scook at elp.rr.com> wrote in message
news:yWv%c.33126$Dl4.19812 at fe2.texas.rr.com...
> Yes, I used the listdir.  The list is a list of files in the
> directory.  I want to filter everything out but the ".dbf"
> files.
>

You said the answer yourself - "I want to _filter_ everything out but the
".dbf" files."

Use filter built-in, and use str's endswith() method in place of [-4:] list
slicing.

dirlist = [ "a.txt", "b.txt", "c.dbf", "d.txt", "e.dbf" ]
isdbf = lambda x : x.endswith(".dbf")
print filter( isdbf, dirlist )

gives:

['c.dbf', 'e.dbf']


-- Paul





More information about the Python-list mailing list