List comprehensions and glob

Duncan Booth me at
Thu Jul 15 11:42:15 CEST 2004

Peter Otten <__peter__ at> wrote in news:cd5gvg$l4u$06$1 at news.t-

> Duncan Booth wrote:
>> If you want a list comprehension then how about:
>> filenames = [ name for pat in PATTERNS for name in glob.glob(pat) ]
>>>> PATTERNS = ["*t", "*xt"]
>>>> filenames = [name for pat in PATTERNS for name in glob.glob(pat)]
>>>> len(filenames), len(sets.Set(filenames))
> (66, 39)
> Names that match more than one pattern will appear more than once in the
> list.
As indeed they do in the original posting. I assumed that either Robin 
wanted that behaviour or knew it wasn't relevant (the example given had 
non-overlapping patterns).

To remove duplicates just pass through a dictionary or a set:

filenames = dict.fromkeys(filenames).keys()

More information about the Python-list mailing list