- Python – Check if String contains Substring from List
- Check if string contains substring(s) from list in Python
- Examples
- 1. Check if given string contains items from list
- Summary
- Check if List contains a substring in Python
- Introduction
- Frequently Asked:
- Check if list contains a substring using any() function
- Check if list contains a substring using List comprehension
- Check if list contains a substring using a For Loop
- Check if list contains a substring using filter() and Lambda functions
- Summary
- Related posts:
- Share your love
- Leave a Comment Cancel Reply
- Terms of Use
- Disclaimer
- Python Check If String Contains Substring From List
- How to Check If String Contains Substring From List in Python?
- Method 1: Check If String Contains Substring From List in Python through List Comprehension
- Example
- Method 2: Check If String Contains Substring From List in Python Using “any()” Method
- Example
- Method 3: Check If String Contains Substring From List in Python Using “for” Loop
- Example
- Conclusion
- About the author
- Maria Naz
Python – Check if String contains Substring from List
Check if string contains substring(s) from list in Python
To check if string contains substring from a list of strings, iterate over list of strings, and for each item in the list, check if the item is present in the given string.
In this type of scenario, we have
- a source string, in which we have to check if some substring is present.
- a list of strings, whose elements may occur in the source string, and we have to find if any of these strings in the list occur in source string.
Examples
1. Check if given string contains items from list
In this example, we will take a source string, and a list of strings. We will use for loop, to check if the string from list is present as substring in source string.
Python Program
source_string = 'a b c d e f' list_of_strings = ['k', 'm', 'e' ] for substring in list_of_strings: if substring in source_string: print('String contains substring from list.') break
String contains substring from list.
As the item from list ‘e’ is present as substring in the source string, during the for loop execution for the element ‘e’, condition in if loop becomes True.
Summary
In this tutorial of Python Examples, we learned how to check if string contains substring from a list of strings, with the help of example program.
Check if List contains a substring in Python
In this article, we will discuss different ways to check if a list contains a substring or not in Python.
Table Of Contents
Introduction
Suppose we have a list of strings. Something like this,
Now we want to check if any string in this list contains a substring – “is” or not. Here, the string “this” in list contains the substring “is”, so yes this list contains the given substring.
There are different ways to check if list contains a substring or not. Let’s discuss them one by one.
Frequently Asked:
Check if list contains a substring using any() function
Iterate over all strings in list, and for each string, check if it contains the given substring or not. If a string contains the given substring then the conditional expression will evaluate to True, otherwise it will evaluate to False. Pass all the resultant values to the any() function, to check if any expression is evaluating to True or not.
The any() function in Python, accepts a sequence of boolean values, and returns True if any value in this boolean list evaluates to True. So, in this case if any() function returns True, it means list has a string that contains the given substring.
Let’s see an example. Here, we will check if any string in this list contains a substring – “is” or not.
# List of strings listOfStrs = ["why", "where", "how", "this", "what"] # A Substring subStr = "is" # check if any string in list contains the given substring if any(subStr in elem for elem in listOfStrs): print('Yes, List contains the substring') else: print('No, List does not contains the substring')
Yes, List contains the substring
Yes, there is atleast a string in the list that contains the given substring.
Check if list contains a substring using List comprehension
Filter the list using List comprehension, and select only those strings which contains the given substring. If the length of this filtered list is more than 0, then it means that list has a string that contains the given substring. Let’s see an example. Here, we will check if any string in this list contains a substring – “is” or not.
# List of strings listOfStrs = ["why", "where", "how", "this", "what"] filteredList = [elem for elem in listOfStrs if "is" in elem] # check if any string in list contains the given substring if len(filteredList) > 0: print('Yes, List contains the substring') else: print('No, List does not contains the substring')
Yes, List contains the substring
Yes, there is atleast a string in the list that contains the given substring.
Check if list contains a substring using a For Loop
Iterate over all the strings in a list using a for loop, and for each string, check if the given substring exists in that string or not. As soon as, it finds a string like that, break the loop. Let’s see an example. Here, we will check if any string in this list contains a substring – “is” or not.
# List of strings listOfStrs = ["why", "where", "how", "this", "what"] # A Sub String subStr = "is" result = False # check if any string in list contains the given substring for elem in listOfStrs: if subStr in elem: result = True break if result: print('Yes, List contains the substring') else: print('No, List does not contains the substring')
Yes, List contains the substring
Yes, there is atleast a string in the list that contains the given substring.
Check if list contains a substring using filter() and Lambda functions
Using the filter() function, and a lambda expression, select only those strings which contains the given substring. If the length of this filtered list is more than 0, then it means that list has a string that contains the given substring. Let’s see an example. Here, we will check if any string in this list contains a substring – “is” or not.
# List of strings listOfStrs = ["why", "where", "how", "this", "what"] # A Sub String subStr = "is" # check if any string in list contains the given substring filteredList = list(filter(lambda elem: subStr in elem, listOfStrs)) if len(filteredList) > 0: print('Yes, List contains the substring') else: print('No, List does not contains the substring')
Yes, List contains the substring
Yes, there is atleast a string in the list that contains the given substring.
Summary
We learned about different ways to check if a list of strings contain a substring or not.
Related posts:
Share your love
Leave a Comment Cancel Reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Terms of Use
Disclaimer
Copyright © 2023 thisPointer
To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Python Check If String Contains Substring From List
In Python, the strings are the combination of characters contained in quotes. Like strings, lists are also used to save a collection of strings. While working with Python, users frequently encounter situations where they must determine if the provided string contains a substring from any given Python list. To resolve this encountered scenario, multiple methods are used.
This guide will provide different techniques to check if a string contains a substring from the list in Python.
How to Check If String Contains Substring From List in Python?
To check if the string has a substring from the list in Python, the following techniques are used:
Method 1: Check If String Contains Substring From List in Python through List Comprehension
To check if the list contains the substring from the Python list, the list comprehension can be used. The implementation of the list comprehension is stated in the below example.
Example
First, create a string variable and pass a string. Then, define and initialize a list:
first_str = «LinuxHint is the world best tutorial website»
var_list = [ ‘forum’ , ‘website’ ]
Now, call the “print()” function to view the input value of string and list:
print ( «My input string is : » + first_str )
print ( «My initialized list is : » + str ( var_list ) )
Use the “for” loop and check the condition with “if” statement from the input string:
Now, view the checked condition use a “bool()” method inside the “print()” statement:
According to the below given output, the provided string contains the substring from the initialized Python list:
Method 2: Check If String Contains Substring From List in Python Using “any()” Method
Another way to check if the string contains a substring from the Python list or not is the “any()” method. This method checks each list element to an input string match.
Example
Call the “any()” method along with the “for” loop to check the list variable and store it into the “resultant_str” variable:
Call the “print()” function to display the filtered result:
Method 3: Check If String Contains Substring From List in Python Using “for” Loop
We can also use the “for” loop for checking if the provided string contains a substring from the list in Python or not. The “for” loop is the iterative function that iterates over the provided sequence of the string element.
Example
Use the “for” loop to check the substring by utilizing the “if” condition, and print the desired result through the print statement:
for substring in var_list:
if substring in first_str:
print ( «String contain a list element» )
It can be observed the provided string contains the substring from the previously initialized Python list:
That’s all! You have learned about the different techniques to view if a string has a substring from the list in Python.
Conclusion
To check if the provided string has the substring from the provided list in Python, the “list comprehension”, the “any()” method, and the iterative function “for” loop are used. All techniques first check the provided condition and then display the results. This guide illustrated multiple ways to check if a string contains a substring from the list in Python.
About the author
Maria Naz
I hold a master’s degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.