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.


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("The second list is :")

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 :")


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']


  • 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.