Check if list of words exists in a String in Python

avatar

Borislav Hadzhiev

Last updated: Sep 19, 2022

banner

Photo from Unsplash

Check if list of words exists in a String in Python #

To check if a list of words exists in a string:

  1. Use a generator expression to iterate over the list.
  2. Check if each word is contained in the string.
  3. If the condition is met for all elements, the words in the list exist in the string.
main.py
list_of_words = ['bobby', 'hadz', 'com'] my_str = 'bobby123' # ✅ Check if any words from list exist in a string any_word_in_string = any(word in my_str for word in list_of_words) print(any_word_in_string) # 👉️ True # ------------------------------------------------------ # ✅ Check if all words from list exist in a string all_words_in_string = all(word in my_str for word in list_of_words) print(all_words_in_string) # 👉️ False # ------------------------------------------------------ # ✅ Find words from list that exist in a string matching_words = [word for word in list_of_words if word in my_str] print(matching_words) # 👉️ ['bobby']

The first example uses the any() function to check if any of the words from the list exist in a string.

main.py
list_of_words = ['bobby', 'hadz', 'com'] my_str = 'bobby123' any_word_in_string = any(word in my_str for word in list_of_words) print(any_word_in_string) # 👉️ True if any_word_in_string: # 👇️ this runs print('Some of the words from the list are contained in the string') else: print('None of the words from the list are contained in the string')

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 use the in operator to check if the current word is 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.

main.py
my_str = 'bobby hadz' print('bobby' in my_str) # 👉️ True print('another' in my_str) # 👉️ False

If you need to check if any of the words from the list are contained in the string, ignoring the case, convert both strings to lowercase.

main.py
list_of_words = ['BOBBY', 'HADZ', 'COM'] my_str = 'bobby123' any_word_in_string = any(word.lower() in my_str.lower() for word in list_of_words) print(any_word_in_string) # 👉️ True

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

If the condition returns True for any of the words from the list, the any() function short-circuits and returns True.

If you need to check if all words from the list are contained in the string, use the all() function instead.

main.py
list_of_words = ['bobby', 'hadz', 'com'] my_str = 'bobbyhadz.com' all_words_in_string = all(word in my_str for word in list_of_words) print(all_words_in_string) # 👉️ True

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 find the words from the list that are contained in the string, use a list comprehension.

main.py
list_of_words = ['bobby', 'hadz', 'com'] my_str = 'bobby 306' matching_words = [word for word in list_of_words if word in my_str] print(matching_words) # 👉️ ['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 words that are contained in the string.

You can perform a case-insensitive membership test by converting both strings to lowercase.

main.py
list_of_words = ['bobby', 'hadz', 'com'] my_str = 'BOBBY 306' matching_words = [word for word in list_of_words if word.lower() in my_str.lower()] print(matching_words) # 👉️ ['bobby']

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

Converting both strings to lowercase or uppercase allows for a case-insensitive comparison.

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.