Q: Elegant way to delete items in a list which do not has substrings that appear in another list Recently I encountered this problem:
Say there is a list of something I want to process:
process_list=["/test/fruit/apple","/test/fruit/pineapple","/test/fruit/banana","/test/tech/apple-pen","/test/animal/python","/test/animal/penguin"]

And I want to exclude something using another list, for instance:
exclude_list=["apple","python"]

The process_list should be like this after I apply the exclude_list to it( any process_list item that contains a sub:
["/test/fruit/banana","/test/animal/penguin","/test/fruit/pineapple"]

or if the exclude_list is:
exclude_list=["pen","banana"]
The process_list should be this after apply the filter:
["/test/fruit/apple","/test/fruit/pineapple","/test/animal/python"]

So what I was trying at first was:
for item in exclude_list:
    for name in (process_list):
        if item in name:
            process_list.remove(name)

Of course this didn't work because removing elements from the list while iterating over it using a for loop is not permitted. The code only removed the first match and then stopped.
So then I came up a way to do this with another list:
deletion_list=[] #Track names that need to be deleted
for item in exclude_list:
    for name in (process_list):
        if item in name:
            deletion_list.append(name)
# A list comprehension
process_list=[ x for x in process_list if x not in deletion_list ]  

It works, but my guts tell me there may be a more elegant way. Now it need s another list to store the name need to be deleted. Any ideas?

A: Just in addition you can also use regular expression e.g.
import re

pattern = '(' + ('|').join(exclude_list) + ')'
list(filter(lambda l : re.search(pattern,l) == None, process_list)) #filter will return iterator in case if you use python 3 


A: Use os.path.basename to get the basename of pathname, use the build-in functon all to check if the basename is not included in exclude_list.
import os 

process_list=["/test/fruit/apple","/test/fruit/pineapple","/test/fruit/banana","/test/tech/apple-pen","/test/animal/python","/test/animal/penguin"]

# Case 1
exclude_list=["apple","python"]

l = [s for s in process_list 
        if all(item not in os.path.basename(s) for item in exclude_list)]

print(l)
['/test/fruit/banana', '/test/animal/penguin']

# Case 2
exclude_list=["pen","banana"]

l = [s for s in process_list 
        if all(item not in os.path.basename(s) for item in exclude_list)]

print(l)
['/test/fruit/apple', '/test/fruit/pineapple', '/test/animal/python']


A: You may use the list comprehension expression using all() filter as:
# Here: `p` is the entry from `process_list`
#       `e` is the entry from `exclude_list`

>>> [p for p in process_list if all(e not in p for e in exclude_list)]                              
['/test/fruit/banana', '/test/animal/penguin']


Regarding your statement:

Of course this didn't work because removing elements from the list while iterating over it using a for loop is not permitted. The code only removed the first match and then stopped.

You could have iterate over the copy of the list as:
 for item in list(exclude_list):  # OR, for item in exclude_list[:]:
 #              ^-- Creates new copy ----------------------------^           


A: [line for line in lines if not any(word in line for word in words)]


A: Another approach to achieve what you want is as follows:
[item for item in process_list if not any(exc in item.split('/')[-1] for exc in exclude_list)]

Output:
>>> [item for item in process_list if not any(exc in item.split('/')[-1] for exc in exclude_list)]
['/test/fruit/banana', '/test/animal/penguin']


A: if you want to work directly with process_list and the list comprehension without any funny behavior you should work with a copy, which is created like this: process_list[:]    
process_list=["/test/fruit/apple","/test/fruit/pineapple","/test/fruit/banana","/test/tech/apple-pen","/test/animal/python","/test/animal/penguin"]
exclude_list=["apple","python"]
process_list = [x for x in  process_list[:] if not any(y in x for y in exclude_list)]

