How to Find a Partial String in a Python List?

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

Wait, forgot to escape a space. Wheeeeee[taptaptap]eeeeee. (source)