How to check if a string contains an element from a list in Python

A string contains an element from a list if some substring of the string matches an item in the list. For example, “abc@gmail.com” contains the element “gmail” in [“gmail”, “hotmail”, “yahoo”].

Solution for How to check if a string contains an element from a list in Python : You can use any() to check if a string contains an element from a list Call any(iterable) with iterable as a generator expression that checks if any item from the list is in the string. Use the syntax element in string for element in list to build the generator expression.


how-to-check-if-a-string-contains-an-element-from-a-list-in-python