09-12-2022, 03:53 PM
How to Find a Partial String in a Python List?
5/5 – (1 vote)
Problem Formulation
Challenge: Given a Python list of strings and a query string. Find the strings that partially match the query string.
Example 1:
Example 2:
Example 3:
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 in a string list is to use the membership operator and the list comprehension statement like so: .
Here’s a simple example:
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
![[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]](https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2F9qsq2Vf48W8%2Fhqdefault.jpg)
Recommended Tutorial: The Membership Operator in Python
![[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]](https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FoQkfuBAk9lo%2Fhqdefault.jpg)
Method 2: list() and filter()
To find a list of partial query matches given a string list, combine the membership operator with the function in which you pass a lambda function that evaluates the membership operation for each element in the list like so: .
Here’s an example:
Beautiful Python one-liner, isn’t it?![[Image: 1f984.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f984.png)
I recommend you check out the following tutorial with video to shed some light on the background information here:
Recommended Tutorial: Python Filtering
![[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]](https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2F3nG4TLkqzf8%2Fhqdefault.jpg)
Generally, I like list comprehension more than the 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 is provided by Python’s powerful regular expressions functionality. For example, the expression finds all strings that match a certain query pattern as defined by you.
The following examples showcase this solution:
In this example, we use the dummy pattern that simply matches words that contain the 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 — A Simple Illustrated Guide
![[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]](https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2F5d3vQ8N0MJg%2Fhqdefault.jpg)
Regex Humor
[i]Wait, forgot to escape a space. Wheeeeee[taptaptap]eeeeee. (source)[/i]
5/5 – (1 vote)
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: andCode:
['hello', 'world', 'python']Code:'pyth'
- Output: Code:
['python']
Example 2:
- Input: andCode:
['aaa', 'aa', 'a']Code:'a'
- Output: Code:
['aaa', 'aa', 'a']
Example 3:
- Input: andCode:
['aaa', 'aa', 'a']Code:'b'
- Output: Code:
[]
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
Code:
queryCode:
lstCode:
inCode:
[s for s in lst if query in s]Here’s a simple example:
Code:
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 PythonMethod 2: list() and filter()
To find a list of partial query matches given a string list
Code:
lstCode:
filter()Code:
list(filter(lambda x: query in x, lst))Here’s an example:
Code:
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?
![[Image: 1f984.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f984.png)
I recommend you check out the following tutorial with video to shed some light on the background information here:
Recommended Tutorial: Python FilteringGenerally, I like list comprehension more than the
Code:
filter()Method 3: Regex Match + List Comprehension
The most flexible way to find a list of partial query matches given a string list
Code:
lstCode:
[x for x in lst if re.match(pattern, x)]The following examples showcase this solution:
Code:
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
Code:
.*query.*Code:
queryAgain, I’d recommend you check out the background info on regular expressions:
Recommended Tutorial: Python Regex Code:
match()Regex Humor
[i]Wait, forgot to escape a space. Wheeeeee[taptaptap]eeeeee. (source)[/i]

