Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Filter rows without Space Strings

Improve Article
Save Article
Like Article
  • Last Updated : 22 Feb, 2023
Improve Article
Save Article
Like Article

Given Matrix, extract rows in which Strings don’t have spaces.

Examples:

Input: test_list = [[“gfg is”, “best”], [“gfg”, “good”], [“gfg is cool”], [“love”, “gfg”]] 
Output: [[‘gfg’, ‘good’], [‘love’, ‘gfg’]] 
Explanation: Both the lists have strings that don’t have spaces.
 

Input: test_list = [[“gfg is”, “best”], [“gfg “, “good”], [“gfg is cool”], [“love”, “gfg”]] 
Output: [[‘love’, ‘gfg’]] 
Explanation: The list has strings that don’t have spaces. 

Method #1: Using list comprehension + any() + regex

In this, we check for no space in each string using regex, any() is used to check this for any string found with spaces, that row is not added.

Python3




# Python3 code to demonstrate working of
# Filter rows without Space Strings
# Using list comprehension + any() + regex
import re
 
# initializing list
test_list = [["gfg is", "best"], ["gfg", "good"],
             ["gfg is cool"], ["love", "gfg"]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# checking for spaces using regex
# not including row if any string has space
res = [row for row in test_list if not any(
    bool(re.search(r"\s", ele)) for ele in row)]
 
# printing result
print("Filtered Rows : " + str(res))

Output

The original list is : [['gfg is', 'best'], ['gfg', 'good'], ['gfg is cool'], ['love', 'gfg']]
Filtered Rows : [['gfg', 'good'], ['love', 'gfg']]

Complexity Analysis:

Time Complexity: O(N2), (loop * re.search())
Auxiliary Space: O(N)

Method #2 : Using filter() + lambda + any() + regex

In this, we perform task of filtering using filter() and lambda function, rest all the functionalities are performed alike the above method.

Python3




# Python3 code to demonstrate working of
# Filter rows without Space Strings
# Using filter() + lambda + any() + regex
import re
 
# initializing list
test_list = [["gfg is", "best"], ["gfg", "good"],
             ["gfg is cool"], ["love", "gfg"]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# checking for spaces using regex
# not including row if any string has space
res = list(filter(lambda row: not any(bool(re.search(r"\s", ele))
                                      for ele in row), test_list))
 
# printing result
print("Filtered Rows : " + str(res))

Output

The original list is : [['gfg is', 'best'], ['gfg', 'good'], ['gfg is cool'], ['love', 'gfg']]
Filtered Rows : [['gfg', 'good'], ['love', 'gfg']]

Complexity Analysis:

Time Complexity: O(N2),  for loop takes the time complexity of O(n) and the filter also takes O(n) so together the final complexity is O(n2),
Auxiliary Space: O(N), the size of array, so O(n)

Method #3 : Using join() and find() methods

In this method, we perform task of joining all the strings using join() method and then checking if there is a space between the string using find() method.

Python3




# Python3 code to demonstrate working of
# Filter rows without Space Strings
 
# initializing list
test_list = [["gfg is", "best"], ["gfg", "good"],
             ["gfg is cool"], ["love", "gfg"]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# checking for spaces using regex
# not including row if any string has space
res = []
for i in test_list:
    a = "".join(i)
    if(a.find(" ") == -1):
        res.append(i)
# printing result
print("Filtered Rows : " + str(res))

Output

The original list is : [['gfg is', 'best'], ['gfg', 'good'], ['gfg is cool'], ['love', 'gfg']]
Filtered Rows : [['gfg', 'good'], ['love', 'gfg']]

Time Complexity: O(n*n)
Auxiliary Space: O(n)

Method #4:Using itertools.filterfalse() method

Python3




# Python3 code to demonstrate working of
# Filter rows without Space Strings
import itertools
import re
 
# initializing list
test_list = [["gfg is", "best"], ["gfg", "good"],
             ["gfg is cool"], ["love", "gfg"]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# checking for spaces using regex
# not including row if any string has space
res = list(itertools.filterfalse(lambda row:  any(bool(re.search(r"\s", ele))
                                      for ele in row), test_list))
 
# printing result
print("Filtered Rows : " + str(res))

Output

The original list is : [['gfg is', 'best'], ['gfg', 'good'], ['gfg is cool'], ['love', 'gfg']]
Filtered Rows : [['gfg', 'good'], ['love', 'gfg']]

Time Complexity:  O(N2)

Auxiliary Space: O(N)

Method #5: Here is a new approach using a list comprehension and the split and all() method:

Python3




# Python3 code to demonstrate working of
# Filter rows without Space Strings
# Using list comprehension + split() method
  
# initializing list
test_list = [["gfg is", "best"], ["gfg", "good"],
             ["gfg is cool"], ["love", "gfg"]]
  
# printing original list
print("The original list is : " + str(test_list))
  
# checking for spaces using the split() method
# not including row if any string has space
res = [row for row in test_list if all(ele.split() == [ele] for ele in row)]
  
# printing result
print("Filtered Rows : " + str(res))

Output

The original list is : [['gfg is', 'best'], ['gfg', 'good'], ['gfg is cool'], ['love', 'gfg']]
Filtered Rows : [['gfg', 'good'], ['love', 'gfg']]

Time Complexity: O(N2), loop * split() method
Auxiliary Space: O(N)


My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!