Python Program to Extract Strings with at least given number of characters from other list

PythonServer Side ProgrammingProgramming

When it is required to extract strings with atleast a given number of characters from the other list, a list comprehension is used.


Below is a demonstration of the same

my_list = ["Python", "is", "fun", "to", "learn"]

print("The list is :")

my_char_list = ['e', 't', 's', 'm', 'n']

my_key = 2
print("The value of key is ")

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


The list is :
['Python', 'is', 'fun', 'to', 'learn']
The value of key is
The resultant list is :
['Python', 'learn']


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

Published on 16-Sep-2021 08:07:26