- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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.
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)
The list is : [[‘python is’, ‘fun’], [‘python’, ‘good’], [‘python is cool’], [‘love’, ‘python’]] The resultant list is : [[‘python’, ‘good’], [‘love’, ‘python’]]
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.
- Python - Filter Supersequence Strings
- 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 - Custom space size padding in Strings List
- Python – Filter Tuples with Strings of specific characters
- Python Program to Filter Rows with a specific Pair Sum
- Python – Filter immutable rows representing Dictionary Keys from Matrix
- Python – Filter rows with Elements as Multiple of K
- Python – Extract rows with Even length strings
- Concatenating two strings in MySQL with space?