Problem Formulation
π¬ Challenge: Given a Python list of strings and a query string. Find the strings that partially match the query string.
Example 1:
- Input:
['hello', 'world', 'python']
and'pyth'
- Output:
['python']
Example 2:
- Input:
['aaa', 'aa', 'a']
and'a'
- Output:
['aaa', 'aa', 'a']
Example 3:
- Input:
['aaa', 'aa', 'a']
and'b'
- Output:
[]
Let’s dive into several methods that solve this and similar type of problems. We start with the most straightforward solution.
Method 1: Membership + List Comprehension
The most Pythonic way to find a list of partial matches of a given string query
in a string list lst
is to use the membership operator in
and the list comprehension statement like so: [s for s in lst if query in s]
.
Here’s a simple example:
def partial(lst, query): return [s for s in lst if query in s] # Example 1: print(partial(['hello', 'world', 'python'], 'pyth')) # ['python'] # Example 2: print(partial(['aaa', 'aa', 'a'], 'a')) # ['aaa', 'aa', 'a'] # Example 3: print(partial(['aaa', 'aa', 'a'], 'b')) # []
In case you need some background information, feel free to check out our two tutorials and the referenced videos.
π Recommended Tutorial: List Comprehension in Python
π Recommended Tutorial: The Membership Operator in Python
Method 2: list() and filter()
To find a list of partial query matches given a string list lst
, combine the membership operator with the filter()
function in which you pass a lambda function that evaluates the membership operation for each element in the list like so: list(filter(lambda x: query in x, lst))
.
Here’s an example:
def partial(lst, query): return list(filter(lambda x: query in x, lst)) # Example 1: print(partial(['hello', 'world', 'python'], 'pyth')) # ['python'] # Example 2: print(partial(['aaa', 'aa', 'a'], 'a')) # ['aaa', 'aa', 'a'] # Example 3: print(partial(['aaa', 'aa', 'a'], 'b')) # []
Beautiful Python one-liner, isn’t it? π¦
I recommend you check out the following tutorial with video to shed some light on the background information here:
π Recommended Tutorial: Python Filtering
Generally, I like list comprehension more than the filter()
function because the former is more concise (e.g., no need to convert the result to a list) and slightly faster. But both work perfectly fine!
Method 3: Regex Match + List Comprehension
The most flexible way to find a list of partial query matches given a string list lst
is provided by Python’s powerful regular expressions functionality. For example, the expression [x for x in lst if re.match(pattern, x)]
finds all strings that match a certain query pattern as defined by you.
The following examples showcase this solution:
import re def partial(lst, query): pattern = '.*' + query + '.*' return [x for x in lst if re.match(pattern, x)] # Example 1: print(partial(['hello', 'world', 'python'], 'pyth')) # ['python'] # Example 2: print(partial(['aaa', 'aa', 'a'], 'a')) # ['aaa', 'aa', 'a'] # Example 3: print(partial(['aaa', 'aa', 'a'], 'b')) # []
In this example, we use the dummy pattern .*query.*
that simply matches words that contain the query
string. However, you could also do more advanced pattern matching—regex to the rescue!
Again, I’d recommend you check out the background info on regular expressions:
π Recommended Tutorial: Python Regex match()
— A Simple Illustrated Guide
Regex Humor


While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.