Check if a String contains an Element from a List in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
6 min

banner

# Table of Contents

  1. Check if a String contains an Element from a List
  2. Check if any element in a List contains a String

# Check if a String contains an Element from a List in Python

Use the any() function to check if a string contains an element from a list.

The any() function will return True if the string contains at least one element from the list and False otherwise.

main.py
my_str = 'one two three' my_list = ['a', 'two', 'c'] if any(substring in my_str for substring in my_list): # ๐Ÿ‘‡๏ธ this runs print('The string contains at least one element from the list') else: print('The string does NOT contain any of the elements in the list')

check if string contains element from list

The code for this article is available on GitHub

If you need to check if any element in a List contains a string, click on the following subheading:

We used a generator expression to iterate over the collection of strings.

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

In the example, we iterate over the list of strings and check if each substring is contained in the string.

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

The any() function will short-circuit returning True if the string contains at least one element from the list.

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.

# Getting the matching substring

You can use the assignment expression syntax if you need to get the list item that is contained in the string.

main.py
my_str = 'one two three' my_list = ['a', 'two', 'c'] if any((match := substring) in my_str for substring in my_list): # ๐Ÿ‘‡๏ธ this runs print('The string contains at least one element from the list') print(match) # ๐Ÿ‘‰๏ธ 'two' else: print('The string does NOT contain any of the elements in the list')

getting the matching substring

The code for this article is available on GitHub
Assignment expressions allow us to assign to variables within an expression using the NAME := expression syntax.

If the iterable we pass to the any() function is empty or none of the elements in the iterable are truthy, the any function returns False.

# Getting all of the matching substrings

If you need to get all of the matching substrings, use a list comprehension.

main.py
my_str = 'one two three' my_list = ['a', 'two', 'c', 'one'] matches = [substring for substring in my_list if substring in my_str] print(matches) # ๐Ÿ‘‰๏ธ ['two', 'one']

getting all matching strings

The code for this article is available on GitHub

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 substring is found in the string and return the result.

The new list contains all of the list's elements that are contained in the string.

# Checking in a case-insensitive manner

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

main.py
my_str = 'ONE TWO THREE' my_list = ['a', 'two', 'c'] if any(substring.lower() in my_str.lower() for substring in my_list): # ๐Ÿ‘‡๏ธ this runs print('The string contains at least one element from the list') else: print('The string does NOT contain any of the elements in the list')
The code for this article is available on GitHub

We used the str.lower() method to convert each list item and the string to lowercase before checking if each list item is contained in the string.

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.

# Check if a String contains an Element from a List using a for loop

You can also use a for loop to check if a string contains an element from a list.

main.py
my_str = 'one two three' my_list = ['a', 'two', 'c'] is_contained = False for substring in my_list: if substring in my_str: is_contained = True break print(is_contained) # ๐Ÿ‘‰๏ธ True if is_contained: # ๐Ÿ‘‡๏ธ this runs print('The string contains at least one element from the list') else: print('The string does NOT contain any of the elements in the list')
The code for this article is available on GitHub

We initialized the is_contained variable to False and used a for loop to iterate over the list of strings.

On each iteration, we check if the current substring is contained in the string.

If the condition is met, we set the is_contained variable to True and exit the for loop.

# Check if any element in a List contains a String

To check if any element in a list contains a string:

  1. Use a generator expression to iterate over the list.
  2. Use the in operator to check if the string is contained in each list item.
  3. If the condition is met at least once, the string is contained in the list.
main.py
my_list = ['bobby', 'hadz', 'com'] substring = 'z' result = any(substring in word for word in my_list) print(result) # ๐Ÿ‘‰๏ธ True if any(substring in word for word in my_list): # ๐Ÿ‘‡๏ธ this runs print('The substring is contained in at least one of the list items') else: print('The substring is NOT contained in any of the list items')
The code for this article is available on GitHub

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

main.py
my_list = ['bobby', 'hadz', 'com'] substring = 'z' result = any(substring in word for word in my_list) print(result) # ๐Ÿ‘‰๏ธ True

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
x not in s returns the negation of x in s.

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

# Check if any element in a List contains a String, ignoring the case

If you need to check if a certain substring is contained in any of the items in a list ignoring the case, convert both strings to lowercase.

main.py
my_list = ['BOBBY', 'HADZ', 'COM'] substring = 'z' result = any(substring.lower() in word.lower() for word in my_list) print(result) # ๐Ÿ‘‰๏ธ True if any(substring.lower() in word.lower() for word in my_list): # ๐Ÿ‘‡๏ธ this runs print('The substring is contained in at least one of the list items') else: print('The substring is NOT contained in any of the list items')

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

The method doesn't change the original string, it returns a new string. Strings are immutable in Python.

We can perform a case-insensitive membership test by converting both strings to lowercase or uppercase.

# Find the list items in which the substring is contained

If you need to find the list items in which the substring is contained, use a list comprehension.

main.py
my_list = ['bobby', 'hadz', 'com'] substring = 'z' matches = [word for word in my_list if substring in word] print(matches) # ๐Ÿ‘‰๏ธ ['hadz']

find list items in which substring is contained

The code for this article is available on GitHub
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 list items in which the substring is contained.

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

main.py
my_list = ['bobby', 'hadz', 'com'] substring = 'Z' matches = [word for word in my_list if substring.lower() in word.lower()] print(matches) # ๐Ÿ‘‰๏ธ ['hadz']

# Check if any element in a List contains a String using a for loop

You can also use a for loop to check if any element in a list contains a string.

main.py
my_list = ['bobby', 'hadz', 'com'] substring = 'z' any_element_contains = False for item in my_list: if substring in item: any_element_contains = True break print(any_element_contains) # ๐Ÿ‘‰๏ธ True if any_element_contains: # ๐Ÿ‘‡๏ธ this runs print('The substring is contained in at least one of the list items') else: print('The substring is NOT contained in any of the list items')
The code for this article is available on GitHub

We initialized the any_element_contains variable to False.

On each iteration, we use the in operator to check if the current item contains the substring.

If the condition is met, we set the any_element_contains variable to True and exit the loop.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev