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.