Find a string containing a substring in a list in Python

avatar

Borislav Hadzhiev

Last updated: Aug 13, 2022

banner

Photo from Unsplash

Find a string containing a substring in a list in Python #

Use a list comprehension to find a string that contains a substring in a list, e.g. all_matches = [item for item in my_list if substring in item]. The list comprehension will return a new list that contains all of the strings that contain the specified substring.

main.py
my_list = ['apple', 'application', 'test', 'hello'] substring = 'app' # ✅ find all strings in list that contain substring all_matches = [item for item in my_list if substring in item] print(all_matches) # 👉️ ['apple', 'application'] # ---------------------------------------------------- # ✅ find first string in list that contains substring if any(substring in (match := item) for item in my_list): print(match) # 👉️ 'apple' # ---------------------------------------------------- # ✅ find all strings in list that contain substring (case-insensitive) all_matches = [item for item in my_list if substring.lower() in item.lower()] print(all_matches) # 👉️ ['apple', 'application']

The first example uses a list comprehension to get all of the strings from a list that contain a specified substring.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we check if the current list item contains the specified substring and return the result.

main.py
my_list = ['apple', 'application', 'test', 'hello'] substring = 'app' all_matches = [item for item in my_list if substring in item] print(all_matches) # 👉️ ['apple', 'application']

The in operator tests for membership. For example, x in s evaluates to True if x is a member of s, otherwise it evaluates to False.

The new list only contains the strings that contain the specified substring.

If you need to get the strings that contain a substring in a list in a case-insensitive manner, convert both strings to lowercase.
main.py
my_list = ['APPLE', 'APPLICATION', 'TEST', 'HELLO'] substring = 'app' all_matches = [item for item in my_list if substring.lower() in item.lower()] print(all_matches) # 👉️ ['APPLE', 'APPLICATION']

The list comprehension returns all of the list items that contain the specified substring, ignoring the case.

You can also convert the results to lowercase if you need to.

main.py
my_list = ['APPLE', 'APPLICATION', 'TEST', 'HELLO'] substring = 'app' all_matches = [item.lower() for item in my_list if substring.lower() in item.lower()] print(all_matches) # 👉️ ['apple', 'application']

The str.lower method returns a copy of the string with all the cased characters converted to lowercase.

To perform a case-insensitive comparison, both strings have to either be lowercase or uppercase.

If you need to find the first string in a list that contains a substring, use the any() function with an assignment expression.

main.py
my_list = ['apple', 'application', 'test', 'hello'] substring = 'app' # ✅ find first string in list that contains substring if any(substring in (match := item) for item in my_list): print(match) # 👉️ 'apple'

Assignment expressions allow us to assign to variables within an expression using the NAME := expression syntax.

The match variable stores the first string in the list that contains the specified substring.

The any function takes an iterable as an argument and returns True if any element in the iterable is truthy.

If the any function finds a matching item, it will short-circuit returning True.

Alternatively, you can use a simple for loop.

main.py
my_list = ['apple', 'application', 'test', 'hello'] substring = 'app' for item in my_list: if substring in item: print(item) # 👉️ 'apple' break

The for loop iterates over the list and checks if the current item contains the specified substring.

Once the item is found, we break out of the loop to avoid unnecessary work.

You can use the same approach to get the first string in a list that contains the specified substring in a case-insensitive manner.

main.py
my_list = ['APPLE', 'APPLICATION', 'TEST', 'HELLO'] substring = 'app' for item in my_list: if substring.lower() in item.lower(): print(item) # 👉️ 'apple' break

We convert both the string of the current iteration and the substring we are checking for to lowercase.

Both strings have to be either lowercase or uppercase to perform a case-insensitive comparison.

Which approach you pick is a matter of personal preference. I'd use a list comprehension if I need to get all strings in a list that contain a substring.

If I only need to get the first string in the list that contains a substring, I'd use a for loop as it is quite easy to read.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.