Python - Substring presence in Strings List
Last Updated :
08 May, 2023
Given list of substrings and list of string, check for each substring, if they are present in any of strings in List.
Input : test_list1 = ["Gfg", "is", "best"], test_list2 = ["I love Gfg", "Its Best for Geeks", "Gfg means CS"]
Output : [True, False, False]
Explanation : Only Gfg is present as substring in any of list String in 2nd list.
Input : test_list1 = ["Gfg", "is", "best"], test_list2 = ["I love Gfg", "It is Best for Geeks", "Gfg means CS"]
Output : [True, True, False]
Explanation : Only Gfg and is are present as substring in any of list String in 2nd list.
Method #1 : Using loop
This is brute way in which this task can be performed. In this, we for, each element in list check if it's substring of any of other list's element.
Python3
# Python3 code to demonstrate working of
# Substring presence in Strings List
# Using loop
# initializing lists
test_list1 = ["Gfg", "is", "Best"]
test_list2 = ["I love Gfg", "Its Best for Geeks", "Gfg means CS"]
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
# using loop to iterate
res = []
for ele in test_list1 :
temp = False
# inner loop to check for
# presence of element in any list
for sub in test_list2 :
if ele in sub:
temp = True
break
res.append(temp)
# printing result
print("The match list : " + str(res))
OutputThe original list 1 : ['Gfg', 'is', 'Best']
The original list 2 : ['I love Gfg', 'Its Best for Geeks', 'Gfg means CS']
The match list : [True, False, True]
Time complexity: O(nlogn), where n is the length of the test_list. The loop takes O(n*n) time
Auxiliary Space: O(n), extra space of size n is required
Method #2 : Using list comprehension + any()
The combination of above functions can be used to solve this problem. In this, we check for any sublist using any() and list comprehension is used to perform iteration.
Python3
# Python3 code to demonstrate working of
# Substring presence in Strings List
# Using list comprehension + any()
# initializing lists
test_list1 = ["Gfg", "is", "Best"]
test_list2 = ["I love Gfg", "Its Best for Geeks", "Gfg means CS"]
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
# any() reduces a nesting
# checks for element presence in all Substrings
res = [True if any(i in j for j in test_list2) else False for i in test_list1]
# printing result
print("The match list : " + str(res))
OutputThe original list 1 : ['Gfg', 'is', 'Best']
The original list 2 : ['I love Gfg', 'Its Best for Geeks', 'Gfg means CS']
The match list : [True, False, True]
The Time and Space Complexity for all the methods are the same:
Time Complexity: O(n)
Space Complexity: O(n)
Method 3: using the built-in set and intersection functions.
steps to implement this approach:
- Convert both test_list1 and test_list2 to sets using the set function.
- Compute the intersection of the two sets using the intersection function.
- Convert the result back to a list using the list function.
- For each string in test_list1, check if it is in the resulting list from step 3.
- Append the result of the check to the final result list.
- Print the final result list.
Python3
# Python3 code to demonstrate working of
# Substring presence in Strings List
# Using set intersection
# initializing lists
test_list1 = ["Gfg", "is", "Best"]
test_list2 = ["I love Gfg", "Its Best for Geeks", "Gfg means CS"]
# convert to sets and compute intersection
set1 = set(test_list1)
set2 = set(" ".join(test_list2).split())
intersection = set1.intersection(set2)
# convert intersection back to list
intersection_list = list(intersection)
# initialize result list
result = []
# check if each string in test_list1 is in the intersection list
for string in test_list1:
if string in intersection_list:
result.append(True)
else:
result.append(False)
# print result
print("The match list : " + str(result))
OutputThe match list : [True, False, True]
Time complexity of this approach is O(n+m), where n is the length of test_list1 and m is the total length of all strings in test_list2.
The auxiliary space complexity is O(n+m) to store the sets and intersection.
Method 4: using numpy:
Algorithm:
- Import the required module numpy as np.
- Initialize two numpy arrays arr1 and arr2 with the given values.
- Print the original lists.
- Create a new numpy array res using list comprehension, where each element in the array is True if the
- corresponding element in arr1 is present in any element of arr2, False otherwise.
- Print the match list.
Python3
import numpy as np
# initializing arrays
arr1 = np.array(["Gfg", "is", "Best"])
arr2 = np.array(["I love Gfg", "Its Best for Geeks", "Gfg means CS"])
# printing original lists
print("The original list 1 : " + str(arr1))
print("The original list 2 : " + str(arr2))
# find if substring is present
res = np.array([any(np.char.find(arr2, i) != -1) for i in arr1])
# printing result
print("The match list : ", res)
#This code is contributed by Vinay Pinjala
Output:
The original list 1 : ['Gfg' 'is' 'Best']
The original list 2 : ['I love Gfg' 'Its Best for Geeks' 'Gfg means CS']
The match list : [ True False True]
Time complexity:
The time complexity is O(nmk), where n is the length of arr1, m is the length of arr2, and k is the length of the longest string in arr2.
Space complexity:
The space complexity is O(nm), as we create a new numpy array with nm elements.
Similar Reads
Python | Substring removal in String list
While working with strings, one of the most used application is removing the part of string with another. Since string in itself is immutable, the knowledge of this utility in itself is quite useful. Here the removing of a substring in list of string is performed. Letâs discuss certain ways in which
5 min read
Count Occurance of Substring in a List of Strings - Python
To count the occurrences of a particular substring in a list of strings in Python, we can use several methods. In this article, we are going to explore different methods to count the existence of a particular substring in a given list.Using sum() and Generator ExpressionThis method uses a generator
2 min read
Python - All substrings Frequency in String
Given a String, extract all unique substrings with their frequency. Input : test_str = "ababa" Output : {'a': 3, 'ab': 2, 'aba': 2, 'abab': 1, 'ababa': 1, 'b': 2, 'ba': 2, 'bab': 1, 'baba': 1} Explanation : All substrings with their frequency extracted. Input : test_str = "GFGF" Output : {'G': 2, 'G
5 min read
Python - All occurrences of substring in string
A substring is a contiguous occurrence of characters within a string. Identifying all instances of a substring is important for verifying various tasks. In this article, we will check all occurrences of a substring in String.Using re.finditer()re.finditer() returns an iterator yielding match objects
3 min read
Python | Get matching substrings in string
The testing of a single substring in a string has been discussed many times. But sometimes, we have a list of potential substrings and check which ones occur in a target string as a substring. Let's discuss certain ways in which this task can be performed. Method #1: Using list comprehension Using l
6 min read
Python | Find last occurrence of substring
Sometimes, while working with strings, we need to find if a substring exists in the string. This problem is quite common and its solution has been discussed many times before. The variation of getting the last occurrence of the string is discussed here. Let's discuss certain ways in which we can fin
8 min read
Python - Remove substring list from String
Our task is to remove multiple substrings from a string in Python using various methods like string replace in a loop, regular expressions, list comprehensions, functools.reduce, and custom loops. For example, given the string "Hello world!" and substrings ["Hello", "ld"], we want to get " wor!" by
3 min read
Extract List of Substrings in List of Strings in Python
Working with strings is a fundamental aspect of programming, and Python provides a plethora of methods to manipulate and extract substrings efficiently. When dealing with a list of strings, extracting specific substrings can be a common requirement. In this article, we will explore five simple and c
3 min read
Python - All occurrences of Substring from the list of strings
Given a list of strings and a list of substring. The task is to extract all the occurrences of a substring from the list of strings. Examples: Input : test_list = ["gfg is best", "gfg is good for CS", "gfg is recommended for CS"] subs_list = ["gfg", "CS"] Output : ['gfg is good for CS', 'gfg is reco
5 min read
Python - Get all substrings of given string
A substring is any contiguous sequence of characters within the string. We'll discuss various methods to extract this substring from a given string by using a simple approach. Using List Comprehension :List comprehension offers a concise way to create lists by applying an expression to each element
3 min read