Remove string from list if from substring list

list, numpy, python, string, substring

Solution

Using `list comprehensions`

>>> [l for l in list_dirs if l.split('\\')[-1] not in unwanted_files]
['C:\\bar\\foo\\.world.txt']

Use `split` to get filename

>>> [l.split('\\')[-1] for l in list_dirs]
['hello.txt', '.world.txt', 'yellow.txt']

Problem

I was wondering what's the most pythonic way to: Having a list of strings and a list of substrings remove the elements of string list that contains any of the substring list. ``` list_dirs = ('C:\\foo\\bar\\hello.txt', 'C:\\bar\\foo\\.world.txt', 'C:\\foo\\bar\\yellow.txt') unwanted_files = ('hello.txt', 'yellow.txt) ``` Desired output: ``` list_dirs = (C:\\bar\\foo\.world.txt') ``` I have tried to implement similar questions such as this, but I'm still struggling making the removal and extend that particular implementation to a list. So far I have done this: ``` for i in arange(0, len(list_dirs)): if 'hello.txt' in list_dirs[i]: list_dirs.remove(list_dirs[i]) ``` This works but probably it's not the more cleaner way and more importantly it does not support a list, if I want remove hello.txt or yellow.txt I would have to use a or. Thanks.

Original source

Related problems