you can also use filter () instead of understanding the list, which may have the advantage that you can easily change your filter function for more flexibility:
>>> l1 = ['test1', 'test2', 'test3', 'test4', 'test5'] >>> l2 = set(['*t1*', '*t4*']) >>> filterfunc = lambda item: not any(fnmatch(item, pattern) for pattern in l2) >>> filter(filterfunc, l1) Out: ['test2', 'test3', 'test5'] >>>
Thus, you can even generalize your filterfunc to work with several sets of templates:
>>> from functools import partial >>> def filterfunc(item, patterns): return not any(pattern in item for pattern in patterns) >>> filter(partial(filterfunc, patterns=l2), l1) Out: ['test2', 'test3', 'test5'] >>> filter(partial(filterfunc, patterns={'t1','test5'}), l1) Out: ['test2', 'test3', 'test4']
And of course, you can easily update filterfunc to accept regular expressions in a set of patterns, for example.
ch3ka
source share