When it is required to extract strings with atleast a given number of characters from the other list, a list comprehension is used.
Example
Below is a demonstration of the same
my_list = ["Python", "is", "fun", "to", "learn"] print("The list is :") print(my_list) my_char_list = ['e', 't', 's', 'm', 'n'] my_key = 2 print("The value of key is ") print(my_key) my_result = [element for element in my_list if sum(ch in my_char_list for ch in element) >= my_key] print("The resultant list is :") print(my_result)
Output
The list is : ['Python', 'is', 'fun', 'to', 'learn'] The value of key is 2 The resultant list is : ['Python', 'learn']
Explanation
A list of strings is defined and is displayed on the console.
Another list of characters is defined.
A value for key is defined and is displayed on the console.
A list comprehension is used to iterate over the elements of the list, and get the sum of characters in the character list.
This is compared with the key element.
If it is greater than or equal to the key value, it is stored in a list and is assigned to a variable.
This is displayed as output on the console.