Last updated: Apr 9, 2024
Reading timeยท6 min
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.
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')
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.
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
.
You can use the assignment expression syntax if you need to get the list item that is contained in the string.
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')
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
.
If you need to get all of the matching substrings, use a list comprehension.
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']
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.
If you need to check if a string contains an element from a list in a case-insensitive manner, convert both strings to lowercase.
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')
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.
for
loopYou can also use a for loop to check if a string contains an element from a list.
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')
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.
To check if any element in a list contains a string:
in
operator to check if the string is contained in each list item.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')
We used a generator expression to iterate over the list.
On each iteration, we check if the substring is contained in the current list item and return the result.
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
.
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.
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.
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.
If you need to find the list items in which the substring is contained, use a list comprehension.
my_list = ['bobby', 'hadz', 'com'] substring = 'z' matches = [word for word in my_list if substring in word] print(matches) # ๐๏ธ ['hadz']
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.
my_list = ['bobby', 'hadz', 'com'] substring = 'Z' matches = [word for word in my_list if substring.lower() in word.lower()] print(matches) # ๐๏ธ ['hadz']
for
loopYou can also use a for
loop to check if any element in a list contains a
string.
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')
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.
You can learn more about the related topics by checking out the following tutorials: