Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Find a Partial String in a Python List?

#1
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:

  • 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

YouTube Video

? Recommended Tutorial: The Membership Operator in Python

YouTube Video

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

YouTube Video

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

YouTube Video


Regex Humor


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



https://www.sickgaming.net/blog/2022/09/...thon-list/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Python Int to String with Trailing Zeros xSicKxBot 0 38 12-01-2025, 05:47 PM
Last Post: xSicKxBot
  [Tut] Wrap and Truncate a String with Textwrap in Python xSicKxBot 0 2,057 09-01-2023, 07:45 PM
Last Post: xSicKxBot
  [Tut] List Comprehension in Python xSicKxBot 0 2,107 08-23-2023, 07:54 PM
Last Post: xSicKxBot
  [Tut] Collections.Counter: How to Count List Elements (Python) xSicKxBot 0 1,956 08-19-2023, 06:03 AM
Last Post: xSicKxBot
  [Tut] Write a Long String on Multiple Lines in Python xSicKxBot 0 1,505 08-17-2023, 11:05 AM
Last Post: xSicKxBot
  [Tut] 5 Effective Methods to Sort a List of String Numbers Numerically in Python xSicKxBot 0 1,556 08-16-2023, 08:49 AM
Last Post: xSicKxBot
  [Tut] Sort a List, String, Tuple in Python (sort, sorted) xSicKxBot 0 1,697 08-15-2023, 02:08 PM
Last Post: xSicKxBot
  [Tut] Python Converting List of Strings to * [Ultimate Guide] xSicKxBot 0 1,608 05-02-2023, 01:17 PM
Last Post: xSicKxBot
  [Tut] Python List of Tuples to DataFrame ? xSicKxBot 0 1,512 04-22-2023, 06:10 AM
Last Post: xSicKxBot
  [Tut] Python List of Dicts to Pandas DataFrame xSicKxBot 0 1,534 04-11-2023, 04:15 AM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016