
- 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 – Remove strings with any non-required character
When it is required to remove strings, which have a non-required character, a list comprehension and the ‘any’ operator is used.
Below is a demonstration of the same −
Example
my_list = ["python", "is", "fun", "to", "learn"] print("The list is :") print(my_list) my_char_list = ['p', 's', 'l'] print("The character list is :") print(my_char_list) my_result = [sub for sub in my_list if not any(element in sub for element in my_char_list )] print("The resultant list is :") print(my_result)
Output
The list is : ['python', 'is', 'fun', 'to', 'learn'] The character list is : ['p', 's', 'l'] The resultant list is : ['fun', 'to']
Explanation
A list of strings is defined and is displayed on the console.
Another list with characters is defined and displayed on the console.
A list comprehension is used to iterate over the elements and check if any element is not present in the list.
This is stored in a list and assigned to a variable.
This is displayed as output on the console.
- Related Articles
- How to remove non-ASCII characters from strings
- Python Program to Remove the nth Index Character from a Non-Empty String
- Remove tuple from list of tuples if not containing any character in Python
- How to match any non-digit character in Python using Regular Expression?\n\n
- Python - Remove non-increasing elements
- Python – Sort by Rear Character in Strings List
- How to remove empty strings from a list of strings in Python?
- How to extract required data from structured strings in Python?
- Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character in C++
- Find the non-digit character with JavaScript Regular Expression
- Find non-word character in a string with JavaScript RegExp
- How to escape any special character in Python regular expression?
- Python – Filter rows with required elements
- K’th Non-repeating Character in Python using List Comprehension and OrderedDict
- Program to find minimum remove required to make valid parentheses in Python

Advertisements