Check if string does not contain any strings from List in Python

avatar

Borislav Hadzhiev

Last updated: Sep 19, 2022

banner

Photo from Unsplash

Check if string does not contain any strings from List in Python #

To check if a string does not contain any strings from a list:

  1. Use a generator expression to iterate over the list.
  2. Check if each list item is not contained in the string.
  3. If the condition is met for all list items, the string doesn't contain any of the strings in the list.
main.py
my_list = ['bobby', 'hadz', 'com'] my_str = 'abc bobby 2468' # ✅ Check if string does NOT contain any string from a list not_contains = all(item not in my_str for item in my_list) print(not_contains) # 👉️ False if not_contains: print('The string does NOT contain any string from the list') else: # 👇️ this runs print('The string contains at least one of the strings from the list') # --------------------------------------------------------------- # ✅ Check if string contains at least one string from a list contains = any(item in my_str for item in my_list) print(contains) # 👉️ True # --------------------------------------------------------------- # ✅ Find list items that are contained in a string matches = [item for item in my_list if item in my_str] print(matches) # 👉️ ['bobby']

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.
main.py
my_list = ['bobby', 'hadz', 'com'] my_str = 'abc bobby 2468' not_contains = all(item not in my_str for item in my_list) print(not_contains) # 👉️ False

On each iteration, we check if the current list item is not contained in the string and return the result.

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.

x not in s returns the negation of x in s.

The all() built-in function takes an iterable as an argument and returns True if all elements in the iterable are truthy (or the iterable is empty).

If you need to check if the string contains at least one of the strings from the list, use the any() function instead.

main.py
my_list = ['bobby', 'hadz', 'com'] my_str = 'abc bobby 2468' contains = any(item in my_str for item in my_list) print(contains) # 👉️ True if contains: print('The string contains at least one of the strings from the list') else: print('The string does NOT contain any of the strings from the list')

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

On each iteration, we check if the current list item is contained in the string and return the result.

If the condition is met for any of the list items, the any() function short-circuits and returns True.

If you need to perform a case-insensitive membership test, convert both strings to lowercase.

main.py
my_list = ['bobby', 'hadz', 'com'] my_str = 'ABC BOBBY 2468' contains = any(item.lower() in my_str.lower() for item in my_list) print(contains) # 👉️ True if contains: print('The string contains at least one of the strings from the list') else: print('The string does NOT contain any of the strings from the list')

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

Convert both strings to lowercase or uppercase allows us to test for membership in a case-insensitive manner.

If you need to find the list items that are contained in the string, use a list comprehension.
main.py
my_list = ['bobby', 'hadz', 'com'] my_str = 'abc bobby 2468' matches = [item for item in my_list if item in my_str] print(matches) # 👉️ ['bobby']

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

The new list contains only the strings that are contained in the other string.

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.