The join() method is used to concatenate a list of strings into a single string. The in keyword is used to check if a substring exists in a string. We can use these two methods to check if a substring exists in a list of strings.
cars_list = ["Toyota", "Tesla", "Ford", "Volvo"] cars_str = "\t".join(cars_list) if ('ord' in cars_str): print("Substring exists in the list") else: print("Substring does not exist in the list")
Substring exists in the list
Explanation of the above code example line by line
Enclose the above code in a function and test for multiple substrings
def is_substring_exist(my_list, sub_str): joined_str = "\t".join(my_list) if (sub_str in joined_str): return True else: return False lst = ["Apple", "Banana", "Papaya"] print(is_substring_exist(lst, "ana")) # -> True print(is_substring_exist(lst, "les")) # -> False
Python has a built-in function called any() which can be used to check if a substring exists in a list of strings. any() is a built-in function that returns True if any element of an iterable is True. If all elements are false, it returns False.
The syntax of any() is:
where iterable is an iterable object (list, tuple, dictionary, set, etc.).
lst = ["Apple", "Banana", "Papaya"] substr_exist = any("pp" in sub for sub in lst) if substr_exist: print("Substring exist") else: print("Substring does not exist")
In this code, we create a list called lst that contains the strings "Apple", "Banana", and "Papaya". Then, we use the any() function to check if any of the substrings in the list contain the string "pp". If any of the substrings do contain "pp", the substr_exist variable will be set to True, and the code will print "Substring exist". If none of the substrings contain "pp", the substr_exist variable will be set to False, and the code will print "Substring does not exist".
If you want to check if a particular substring exists in a list of strings, you can do so using the next() function. This function will return the next element in the list that contains the substring you are looking for.
Here we will show you how to return True if the substring matched in the list of strings and False if the substring does not exist in the list.
names_list = ["John", "Mohit", "Sumit", "Sonia"] q_str = "mit" substr_exist = next((True for name in names_list if q_str in name), False) print(substr_exist)
Explanation of the above code example
This program will return the item in the list of strings that contains the substring that is inputted. This is useful if you have a list of strings and you want to find a specific string in the list. Check the below Python program.
names_list = ["John", "Mohit", "Sumit", "Sonia"] q_str = "mit" result = [name for name in names_list if q_str in name] print(result)
This code example is using a list comprehension to create a new list, result, from the names_list. The list comprehension is checking to see if the string, q_str, is a substring of each name in the names_list. If it is, then that name is added to the new list, result.