Python | Test if string contains element from list
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 list element similarity
- Python | Test if any list element returns true for condition
- Python | Replace elements in second list with index of same element in first list
- Python | Indices list of matching element from other list
- Python | Slicing list from Kth element to last element
- Python | Move one list element to another list
- Python | Convert list of string to list of list
- Python | Convert list of string into sorted list of integer
- Python | Convert list of numerical string to list of Integers
- Python | Convert a string representation of list into list
- Python | Convert string enclosed list to list
- Python | Test for nested list
- Python | Test for False list
- Python | Check if given string can be formed by concatenating string elements of list
- Python | Test if all elements are present in list
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.



