Open In App

Filter a list based on the Given List of Strings – Python

Last Updated : 13 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The task of filtering a list based on given strings involves removing elements that contain specific keywords. Given a list of file paths and filter words, the goal is to exclude paths containing any specified words. For example, with filter words [‘key’, ‘keys’, ‘keyword’] and file paths [‘home/key/1.pdf’, ‘home/Desktop/5.pdf’], the filtered list becomes [‘home/Desktop/5.pdf’].

Using set()

This method converts the list of filter words into a set, allowing quick lookups when checking elements in the main list. Since set operations are much faster than list searches, this approach improves performance significantly. It avoids unnecessary loops and is highly efficient for large datasets.

Python
# list of filter words
a = ['key', 'keys', 'keyword', 'keychain', 'keynote']

 # List of file paths
b = ['home/key/1.pdf',
     'home/keys/2.pdf', 
     'home/keyword/3.pdf', 
     'home/keychain/4.pdf', 
     'home/Desktop/5.pdf', 
     'home/keynote/6.pdf']

a_set = set(a) # Convert `a`to set

res = [elem for elem in b if not any(n in elem for n in a_set)]

print(res)

Output
['home/Desktop/5.pdf']

Explanation: list comprehension iterates through b and keeps only those elements that do not contain any word from a_set and any() checks if any word from a_set is present in an element of b. If a match is found, the element is excluded, otherwise it is included in res.

Using re

This method creates a single regex pattern from the filter words and scans the main list in one pass. Instead of checking each element separately, regex efficiently finds matches, reducing repeated comparisons. It is especially useful when dealing with long lists of text data.

Python
import re

# List of filter words
a = ['key', 'keys', 'keyword', 'keychain', 'keynote']

# List of file paths
b = ['home/key/1.pdf', 
     'home/keys/2.pdf', 
     'home/keyword/3.pdf', 
     'home/keychain/4.pdf', 
     'home/Desktop/5.pdf', 
     'home/keynote/6.pdf']

# Create regex pattern from list `a`
pattern = re.compile(r'|'.join(map(re.escape, a)))

res = [elem for elem in b if not pattern.search(elem)]

print(res)

Output
['home/Desktop/5.pdf']

Explanation :re.escape() ensures special characters in the filter list are treated as plain text. ‘|’.join(map(re.escape, a)) creates a regex pattern using the OR (|) operator. The list comprehension then keeps only file paths where pattern.search(elem) finds no match, effectively removing those containing words from the filter list.

Using list comprehension

A simple and readable way to filter a list, this method checks each element against all filter words using any(). Though it requires multiple comparisons, it is easy to implement and works well for smaller datasets where performance is not a major concern.

Python
# List of filter words
a = ['key', 'keys', 'keyword', 'keychain', 'keynote']

# List of file paths
b = ['home/key/1.pdf', 
     'home/keys/2.pdf', 
     'home/keyword/3.pdf', 
     'home/keychain/4.pdf', 
     'home/Desktop/5.pdf', 
     'home/keynote/6.pdf']

res = [elem for elem in b if not any(n in elem for n in a)]

print(res)

Output
['home/Desktop/5.pdf']

Explanation: list comprehension iterates over the file paths and checks if any word from the filter list appears in each path using any(). If a match is found, that path is excluded from the result. The remaining paths are stored in res .

Using filter()

This method applies a filtering condition using the filter() combined with a lambda expression. While it functions similarly to list comprehension, it may introduce slight overhead due to extra function calls. It offers a functional programming approach while keeping the code concise.

Python
# List of filter words
a = ['key', 'keys', 'keyword', 'keychain', 'keynote']

# List of file paths
b = ['home/key/1.pdf', 
     'home/keys/2.pdf', 
     'home/keyword/3.pdf', 
     'home/keychain/4.pdf', 
     'home/Desktop/5.pdf', 
     'home/keynote/6.pdf']

res = list(filter(lambda elem: not any(n in elem for n in a), b))

print(res)

Output
['home/Desktop/5.pdf']

Explanation: filter() with a lambda function, where any(n in elem for n in a) checks if any keyword is present in a file path. If a match is found, the path is excluded. The remaining paths are converted into a list .



Next Article
Practice Tags :

Similar Reads