Check if a list contains a substring in Python

avatar

Borislav Hadzhiev

Last updated: Aug 13, 2022

banner

Photo from Unsplash

Check if a list contains a substring in Python #

Use the any() function to check if a list contains a substring, e.g. if any(substring in item for item in my_list):. The any() function will return True if the list contains the substring and False otherwise.

main.py
my_list = ['apple', 'banana', 'kiwi'] substring = 'app' if any(substring in item for item in my_list): # 👇️ this runs print('The substring is contained in the list') else: print('The substring is NOT contained in the list') # 👇️ True print(any(substring in item for item in my_list))

We used a generator expression to iterate over the list.

Generator expressions 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 a substring is contained in the current list item and return the result.

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

main.py
my_list = ['apple', 'banana', 'kiwi'] substring = 'app' if any(substring in item for item in my_list): # 👇️ this runs print('The substring is contained in the list')

If the any() function encounters a string that contains the specified substring, it will short-circuit returning True.

If the iterable is empty or none of the elements in the iterable are truthy, the any function returns False.

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.

If you need to check if a list contains a substring in a case-insensitive manner, convert both strings to lowercase.

main.py
my_list = ['APPLE', 'BANANA', 'KIWI'] substring = 'app' if any(substring.lower() in item.lower() for item in my_list): # 👇️ this runs print('The substring is contained in the list') else: print('The substring is NOT contained in the list') # 👇️ True print(any(substring.lower() in item.lower() for item in my_list))

We used the str.lower() method to convert the substring and the string to lowercase.

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 filter out all of the list items that don't contain the specified substring, use a list comprehension.

main.py
my_list = ['apple', 'banana', 'kiwi'] substring = 'app' matching_items = [item for item in my_list if substring in item] # 👇️ ['apple'] print(matching_items)

The list comprehension iterates over the list and filters out the items that don't contain the substring.

If your list contains items that are not of type string, convert them to strings before using the in operator.

main.py
my_list = ['apple', 100, 'banana', 500, 'kiwi'] substring = 'ki' if any(substring in str(item) for item in my_list): # 👇️ this runs print('The substring is contained in the list') else: print('The substring is NOT contained in the list')

We used the str() class to convert each list item to a string before using the in operator.

This is necessary because some types, e.g. integers and floats, are not iterable.

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.