Python - All occurrences of Substring from the list of strings
Last Updated :
20 Mar, 2023
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 recommended for CS']
Explanation : Result strings have both "gfg" and "CS".
Input : test_list = ["gfg is best", "gfg is recommended for CS"]
subs_list = ["gfg"]
Output : ["gfg is best", "gfg is recommended for CS"]
Explanation : Result strings have "gfg".
Method #1 : Using loop + in operator
The combination of the above functions can be used to solve this problem. In this, we run a loop to extract all strings and also all substring in the list. The in operator is used to check for substring existence.
Python3
# Python3 code to demonstrate working of
# Strings with all Substring Matches
# Using loop + in operator
# initializing list
test_list = ["gfg is best", "gfg is good for CS",
"gfg is recommended for CS"]
# printing original list
print("The original list is : " + str(test_list))
# initializing Substring List
subs_list = ["gfg", "CS"]
res = []
for sub in test_list:
flag = 0
for ele in subs_list:
# checking for non existence of
# any string
if ele not in sub:
flag = 1
break
if flag == 0:
res.append(sub)
# printing result
print("The extracted values : " + str(res))
Output:
The original list is : ['gfg is best', 'gfg is good for CS', 'gfg is recommended for CS'] The extracted values : ['gfg is good for CS', 'gfg is recommended for CS']
Time Complexity: O(n2)
Auxiliary Space: O(n)
Method #2 : Using all() + list comprehension
This is a one-liner approach with the help of which we can perform this task. In this, we check for all values existence using all(), and list comprehension is used to iteration of all the containers.
Python3
# Python3 code to demonstrate working of
# Strings with all Substring Matches
# Using all() + list comprehension
# initializing list
test_list = ["gfg is best", "gfg is good for CS",
"gfg is recommended for CS"]
# printing original list
print("The original list is : " + str(test_list))
# initializing Substring List
subs_list = ["gfg", "CS"]
# using all() to check for all values
res = [sub for sub in test_list
if all((ele in sub) for ele in subs_list)]
# printing result
print("The extracted values : " + str(res))
Output:
The original list is : ['gfg is best', 'gfg is good for CS', 'gfg is recommended for CS'] The extracted values : ['gfg is good for CS', 'gfg is recommended for CS']
Time Complexity: O(n2)
Auxiliary Space: O(n)
Method #3: Using Counter() function
Python3
# Python3 code to demonstrate working of
# Strings with all Substring Matches
# Using Counter() function
from collections import Counter
# initializing list
test_list = ["gfg is best", "gfg is good for CS",
"gfg is recommended for CS"]
# printing original list
print("The original list is : " + str(test_list))
# initializing Substring List
subs_list = ["gfg", "CS"]
res = []
for sub in test_list:
flag = 0
freq = Counter(sub.split())
for ele in subs_list:
# checking for non existence of
# any string
if ele not in freq.keys():
flag = 1
break
if flag == 0:
res.append(sub)
# printing result
print("The extracted values : " + str(res))
OutputThe original list is : ['gfg is best', 'gfg is good for CS', 'gfg is recommended for CS']
The extracted values : ['gfg is good for CS', 'gfg is recommended for CS']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4: Using set() and intersection() function
Step by step Algorithm:
- Initialize the original list test_list and the list of substrings subs_list.
- Create a new empty list res to store the extracted values.
- Loop through each string sub in test_list.
- Convert sub into a set of words using the split() function.
- Check if subs_list is a subset of the set sub_words using the issubset() function.
- If subs_list is a subset of sub_words, append sub to res.
- After looping through all the strings in test_list, print the extracted values in res.
Python3
test_list = ["gfg is best", "gfg is good for CS", "gfg is recommended for CS"]
subs_list = {"gfg", "CS"}
# printing original list
print("The original list is : " + str(test_list))
res = [sub for sub in test_list if subs_list.issubset(set(sub.split()))]
print("The extracted values : " + str(res))
OutputThe original list is : ['gfg is best', 'gfg is good for CS', 'gfg is recommended for CS']
The extracted values : ['gfg is good for CS', 'gfg is recommended for CS']
Time Complexity: O(n * m * k)
The loop through test_list takes O(n) time, where n is the length of test_list and the conversion of sub into a set of words using the split() function takes O(m) time, where m is the number of words in sub and also the issubset() function takes O(k) time, where k is the number of substrings in subs_list. Thus, the overall time complexity of the algorithm is O(n * m * k).
Space Complexity:
The space used by the res list is O(n*m), where n is the length of test_list and m is the average length of the strings in the list.
Similar Reads
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
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 | Get the starting index for all occurrences of given substring
Given a string and a substring, the task is to find out the starting index for all the occurrences of a given substring in a string. Let's discuss a few methods to solve the given task. Method #1: Using Naive Method Python3 # Python3 code to demonstrate # to find all occurrences of substring in # a
3 min read
Python | Get the string after occurrence of given substring
The problem involves getting the string that is occurring after the substring has been found. Let's discuss certain ways in which this task can be performed using Python.Using partition()To extract the portion of a string that occurs after a specific substring partition() method is an efficient and
3 min read
Python - Filter list of strings based on the substring list
The problem requires to check which strings in the main list contain any of the substrings from a given list and keep only those that match. Let us explore this problem and understand different methods to solve it.Using list comprehension with any() (Most Efficient)List comprehension is a concise an
4 min read
Python | Ways to find nth occurrence of substring in a string
Given a string and a substring, write a Python program to find the nth occurrence of the string. Let's discuss a few methods to solve the given task. Get Nth occurrence of a substring in a String using regex Here, we find the index of the 'ab' character in the 4th position using the regex re.findit
4 min read
Create List of Substrings from List of Strings in Python
In Python, when we work with lists of words or phrases, we often need to break them into smaller pieces, called substrings. A substring is a contiguous sequence of characters within a string. Creating a new list of substrings from a list of strings can be a common task in various applications. In th
3 min read
Python program to find the occurrence of substring in the string
Given a list of words, extract all the indices where those words occur in the string. Input : test_str = 'geeksforgeeks is best for geeks and cs', test_list = ["best", "geeks"] Output : [2, 4] Explanation : best and geeks occur at 2nd and 4th index respectively. Input : test_str = 'geeksforgeeks is
4 min read
Get Second Occurrence of Substring in Python String
We are given a string and a substring, and our task is to find the index of the second occurrence of that substring within the string. This means we need to identify not just if the substring exists, but where it appears for the second time. For example, if we have a string like "hello world, hello
2 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