

- 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 – Filter rows without Space Strings
When it is required to filter rows without the soace strings, a list comprehension, regular expression, the ‘not’ operator and the ‘any’ method are used.
Example
Below is a demonstration of the same
import re my_list = [["python is", "fun"], ["python", "good"],["python is cool"],["love", "python"]] print("The list is :") print(my_list) my_result = [row for row in my_list if not any(bool(re.search(r"\s", element)) for element in row)] print("The resultant list is :") print(my_result)
Output
The list is : [[‘python is’, ‘fun’], [‘python’, ‘good’], [‘python is cool’], [‘love’, ‘python’]] The resultant list is : [[‘python’, ‘good’], [‘love’, ‘python’]]
Explanation
The required packages are imported into the environment.
A list of list is defined and is displayed on the console.
A list comprehension is used to iterate over the list and the ‘search’ method from the regular expression is used to check for a string that doesn’t have space.
The ‘any’ method and ‘not’ operator are used so that any of the strings could be filtered.
This result is assigned to a variable.
This is displayed as output on the console.
- Related Questions & Answers
- Python – Filter Sorted Rows
- Python – Filter Similar Case Strings
- Filter the rows – Python Pandas
- Python – Filter Strings within ASCII range
- Python – Filter rows with required elements
- Python – Filter Rows with Range Elements
- Python – Filter Tuples with Strings of specific characters
- Python – Extract rows with Even length strings
- Python - Filter Supersequence Strings
- Python – Filter immutable rows representing Dictionary Keys from Matrix
- Python – Filter rows with Elements as Multiple of K
- Python – Filter rows with only Alphabets from List of Lists
- Python – Filter Tuples with Integers
- Python – Filter unique valued tuples
- Python – Filter consecutive elements Tuples
Advertisements