
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python - Find all the strings that are substrings to the given list of strings
When it is required to find all the strings that are substrings of a given list of strings, the ‘set’ and ‘list’ attributes are used.
Example
Below is a demonstration of the same
my_list_1 = ["Hi", "there", "how", "are", "you"] my_list_2 = ["Hi", "there", "how", "have", "you", 'been'] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) my_result = list(set([elem_1 for subset_1 in my_list_1 for elem_1 in my_list_2 if elem_1 in subset_1])) print("The result is :") print(my_result)
Output
The first list is : ['Hi', 'there', 'how', 'are', 'you'] The second list is : ['Hi', 'there', 'how', 'have', 'you', 'been'] The result is : ['there', 'you', 'Hi', 'how']
Explanation
Two list of strings are defined and they are displayed on the console.
The two lists are iterated over, and ‘set’ attribute is used to get the unique values from the lists.
This is now converted into a list.
This is assigned to a variable.
This is the output that is displayed on the console.
- Related Articles
- Program to find out the substrings of given strings at given positions in a set of all possible substrings in python
- Python – Strings with all given List characters
- Python Program – Strings with all given List characters
- Program to find size of common special substrings of two given strings in Python
- Python – All occurrences of Substring from the list of strings
- Python program – All occurrences of Substring from the list of strings
- Converting all strings in list to integers in Python
- Find all distinct palindromic sub-strings of a given String in Python
- Python – Find the sum of Length of Strings at given indices
- Python – Sort given list of strings by part the numeric part of string
- Find all palindromic sub-strings of a given string - Set 2 in Python
- How to remove empty strings from a list of strings in Python?
- Python – Sort given list of strings by numeric part of string
- Find strings that end with a given suffix
- Convert list of strings to list of tuples in Python

Advertisements