Python - Test if string contains element from list Last Updated : 11 Jul, 2025 Comments Improve Suggest changes 3 Likes Like Report Testing if string contains an element from list is checking whether any of the individual items in a list appear within a given string.Using any() with a generator expressionany() is the most efficient way to check if any element from the list is present in the list. Python s = "Python is powerful and versatile." el = ["powerful", "versatile", "fast"] # Check if any element in the list exists in the string # using any() and a generator expression res = any(elem in s for elem in el) print(res) OutputTrue Explanation:The any() function evaluates if at least one element in the generator expression is True.The generator expression iterates through the list and checks each element’s presence in the string using the 'in' operator.This approach is efficient as it short-circuits and stops processing once a match is found.Let's explore some more methods to check how we can test if string contains elements from a list.Table of ContentUsing a for loopUsing set intersectionUsing regular expressionsUsing a for loopThis approach explicitly iterates through the list using a for loop to check for the presence of elements in the string. Python s = "Python is powerful and versatile." el = ["powerful", "versatile", "fast"] # Initialize the result variable to False res = False # Iterate through each element in the list for elem in el: if elem in s: res = True break print(res) OutputTrue Explanation:The loop iterates through each element in the list 'el' and checks if it exists in the string 's' using the 'in' operator.If a match is found, the loop exits early using break, which saves unnecessary iterations.Using set intersectionUsing set intersection method is effective when both the string and the list of elements are relatively short. Python s = "Python is powerful and versatile." el = ["powerful", "versatile", "fast"] # Split the string into individual words using the split() method' res = bool(set(s.split()) & set(el)) print(res) OutputTrue Explanation:The split() method breaks the string into individual words and sets are created from the string and list elements.The & operator computes the intersection of the two sets to check for common elements.Using regular expressionsRegular expressions provide flexibility for more complex matching scenarios but are less efficient for simple tasks. Python import re s = "Python is powerful and versatile." el = ["powerful", "versatile", "fast"] # Compile a regular expression pattern to search for any of the elements in the list pattern = re.compile('|'.join(map(re.escape, el))) res = bool(pattern.search(s)) print(res) OutputTrue Explanation:The join() method creates a single pattern from the list of elements, separated by | (logical OR).The re.compile() function compiles the pattern for faster matching and search checks for its presence in the string.This method is less efficient for simple substring checks due to overhead from compiling patterns. Comment M manjeet_04 Follow 3 Improve M manjeet_04 Follow 3 Improve Article Tags : Python Python Programs Python list-programs Python string-programs Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 5 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 7 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like