Python - Find all the strings that are substrings to the given list of strings
Last Updated :
21 Apr, 2023
Given two lists, the task is to write a Python program to extract all the strings which are possible substring to any of strings in another list.
Example:
Input : test_list1 = ["Geeksforgeeks", "best", "for", "geeks"], test_list2 = ["Geeks", "win", "or", "learn"]
Output : ['Geeks', 'or']
Explanation : "Geeks" occurs in "Geeksforgeeks string as substring.
Input : test_list1 = ["geeksforgeeks", "best", "4", "geeks"], test_list2 = ["Geeks", "win", "or", "learn"]
Output : []
Explanation : No substrings found.
Method #1: Using list comprehension
In this, we perform task of using nested loop and testing using list comprehension, extracting the string if its part of any substring of other list.
Python3
# Python3 code to demonstrate working of
# Substring Intersections
# Using list comprehension
# initializing lists
test_list1 = ["Geeksforgeeks", "best", "for", "geeks"]
test_list2 = ["Geeks", "win", "or", "learn"]
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
# using list comprehension for nested loops
res = list(
set([ele1 for sub1 in test_list1 for ele1 in test_list2 if ele1 in sub1]))
# printing result
print("Substrings Intersections : " + str(res))
OutputThe original list 1 is : ['Geeksforgeeks', 'best', 'for', 'geeks']
The original list 2 is : ['Geeks', 'win', 'or', 'learn']
Substrings Intersections : ['or', 'Geeks']
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #2: Using any() + generator expression
In this, any() is used to check for substring matching in any of the strings from the string list to be matched in.
Python3
# Python3 code to demonstrate working of
# Substring Intersections
# Using any() + generator expression
# initializing lists
test_list1 = ["Geeksforgeeks", "best", "for", "geeks"]
test_list2 = ["Geeks", "win", "or", "learn"]
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
# any() returns the string after match in any string
# as Substring
res = [ele2 for ele2 in test_list2 if any(ele2 in ele1 for ele1 in test_list1)]
# printing result
print("Substrings Intersections : " + str(res))
OutputThe original list 1 is : ['Geeksforgeeks', 'best', 'for', 'geeks']
The original list 2 is : ['Geeks', 'win', 'or', 'learn']
Substrings Intersections : ['Geeks', 'or']
Time Complexity: O(n2)
Space Complexity: O(n)
Method #3: Using find() method
Python3
# Python3 code to demonstrate working of
# Substring Intersections
# initializing lists
test_list1 = ["Geeksforgeeks", "best", "for", "geeks"]
test_list2 = ["Geeks", "win", "or", "learn"]
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
res=[]
for i in test_list2:
for j in test_list1:
if(j.find(i)!=-1 and i not in res):
res.append(i)
# printing result
print("Substrings Intersections : " + str(res))
OutputThe original list 1 is : ['Geeksforgeeks', 'best', 'for', 'geeks']
The original list 2 is : ['Geeks', 'win', 'or', 'learn']
Substrings Intersections : ['Geeks', 'or']
Time Complexity: O(n*n)
Auxiliary Space: O(n)
Method#4: Using Recursive method.
we define a recursive function substring_intersections() that takes in two lists of strings as input. It first checks if the second list list2 is empty. If it is, the function returns an empty list [].
If the second list list2 is not empty, the function checks if the first string in list2 is a substring of any element in the first list list1. If it is, it appends it to the result list res.
The function then recursively calls itself with the first element of list2 removed and the same first list list1. The result of this recursive call is concatenated with the result list res.
Finally, the function returns the result list res.
Python3
# printing resultdef substring_intersections(list1, list2):
# Substring Intersections
def substring_intersections(list1, list2):
if not list2:
return []
res = []
for ele1 in list1:
if list2[0] in ele1:
res.append(list2[0])
break
res += substring_intersections(list1, list2[1:])
return res
# initializing lists
test_list1 = ["Geeksforgeeks", "best", "for", "geeks"]
test_list2 = ["Geeks", "win", "or", "learn"]
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
res = substring_intersections(test_list1, test_list2)
# printing result
print("Substrings Intersections : " + str(res))
#this code contributed by tvsk
OutputThe original list 1 is : ['Geeksforgeeks', 'best', 'for', 'geeks']
The original list 2 is : ['Geeks', 'win', 'or', 'learn']
Substrings Intersections : ['Geeks', 'or']
The time complexity of this recursive approach is O(n * m * k) where n, m and k are the lengths of the two input lists and the maximum length of a string in the input lists.
The space complexity is O(k) where k is the maximum length of a string in the input lists.
Method 5 : use is the set intersection method.
steps for this approach:
Convert both test_list1 and test_list2 to sets, which removes any duplicate elements and makes checking for intersections faster.
Take the intersection of the two sets using the "&" operator.
Convert the resulting set back to a list.
Python3
# Python3 code to demonstrate working of
# Substring Intersections
# initializing lists
test_list1 = ["Geeksforgeeks", "best", "for", "geeks"]
test_list2 = ["Geeks", "win", "or", "learn"]
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
res=[]
for i in test_list2:
for j in test_list1:
if(j.find(i)!=-1 and i not in res):
res.append(i)
# printing result
print("Substrings Intersections : " + str(res))
OutputThe original list 1 is : ['Geeksforgeeks', 'best', 'for', 'geeks']
The original list 2 is : ['Geeks', 'win', 'or', 'learn']
Substrings Intersections : ['Geeks', 'or']
The time complexity of this approach is O(mnk), where m and n are the lengths of test_list1 and test_list2, respectively, and k is the maximum length of a substring in either list.
The space complexity is O(k) for storing the res list.
Similar Reads
Python Tutorial | Learn Python Programming Language
Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers
Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts
Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced
Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions
Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs
Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Enumerate() in Python
enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read
Python Data Types
Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Python Introduction
Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Lists
In Python, a list is a built-in dynamic sized array (automatically grows and shrinks). We can store all types of items (including another list) in a list. A list may contain mixed type of items, this is possible because a list mainly stores references at contiguous locations and actual items maybe s
6 min read