

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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
- Extract only characters from given string in Python
- Python program to Sort a List of Strings by the Number of Unique Characters
- Convert list of strings and characters to list of characters in Python
- Python program to extract Keywords from a list
- Program to find elements from list which have occurred at least k times in Python
- Largest Number At Least Twice of Others in Python
- Program to return number of smaller elements at right of the given list in Python
- Longest Substring with At Least K Repeating Characters in C++
- Find the number of binary strings of length N with at least 3 consecutive 1s in C++
- Python Program that extract words starting with Vowel From A list
- Python – Filter all uppercase characters from given list of tuples
Advertisements