
- 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 Program to Extract Strings with at least given number of characters from other list
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.
- Related Articles
- Python Program – Strings with all given List characters
- Python program to extract characters in given range from a string list
- Python – Strings with all given List characters
- Extract numbers from list of strings in Python
- Python program to Sort a List of Strings by the Number of Unique Characters
- Extract only characters from given string in Python
- Python Program to Extract Strings with a digit
- Convert list of strings and characters to list of characters in Python
- Python program to extract Keywords from a list
- Python Program that extract words starting with Vowel From A list
- Program to find elements from list which have occurred at least k times in Python
- Python – Filter all uppercase characters from given list of tuples
- Program to return number of smaller elements at right of the given list in Python
- Python program to extract ‘k’ bits from a given position?
- Program to get final string after shifting characters with given number of positions in Python

Advertisements