During web development in Python, we generally come across a problem where we need to test if a particular element from a given list lies as sub-string or not. This kind of problem is also very common in Machine Learning domain. Let’s discuss certain ways in which this can be done.
Method #1 : Using list comprehension
This problem can be solved using the list comprehension, in this, we check for the list and also with string elements if we can find a match, and return true, if we find one and false is not using the conditional statements.
# Python3 code to demonstrate # checking if string contains list element # using list comprehension # initializing string test_string = "There are 2 apples for 4 persons" # initializing test list test_list = ['apples', 'oranges'] # printing original string print("The original string : " + test_string) # printing original list print("The original list : " + str(test_list)) # using list comprehension # checking if string contains list element res = [ele for ele in test_list if(ele in test_string)] # print result print("Does string contain any list element : " + str(bool(res))) |
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True
Method #2 : Using any()
Using any function is the most classical way in which you can perform this task and also efficiently. This function checks for match in string with match of each element of list.
# Python3 code to demonstrate # checking if string contains list element # using list comprehension # initializing string test_string = "There are 2 apples for 4 persons" # initializing test list test_list = ['apples', 'oranges'] # printing original string print("The original string : " + test_string) # printing original list print("The original list : " + str(test_list)) # using list comprehension # checking if string contains list element res = any(ele in test_string for ele in test_list) # print result print("Does string contain any list element : " + str(res) |
The original string : There are 2 apples for 4 persons The original list : ['apples', 'oranges'] Does string contain any list element : True
Recommended Posts:
- Python - Test if List contains elements in Range
- Python | Test if String contains Alphabets and Spaces
- Python - Test if String contains any Uppercase character
- Python | Convert List of String List to String List
- Python | Test if dictionary contains unique keys and values
- Python - Test if Tuple contains K
- Python program to check if the list contains three consecutive common numbers in Python
- Python | Test list element similarity
- Python | Test if any list element returns true for condition
- Python - Test if greater than preceding element in Tuple List
- Python | Convert list of string to list of list
- Python | Check if list contains all unique elements
- Python | Check if list contains consecutive numbers
- Python - Test String in Character List and vice-versa
- Python program to check if a string contains all unique characters
- Python | Check whether string contains only numbers or not
- Python | Ways to check if given string contains only letter
- Python program to verify that a string only contains letters, numbers, underscores and dashes
- Python - Check if string contains any number
- Python | Slicing list from Kth element to last element
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

