Open In App

Python - Filter rows without Space Strings

Last Updated : 26 Apr, 2023
Comments
Improve
Suggest changes
1 Like
Like
Report

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.


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.


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.


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


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:


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)

Method #6: Using nested loops and flag variable

Step by step approach:

  • Initialize a list of lists called test_list with some test data.
  • Print the original list using the print() function and string concatenation.
  • Initialize an empty list called res to store the filtered rows.
  • For each row in test_list, do the following:
  • Initialize a flag variable called flag to True.
  • For each element (ele) in the current row, do the following:
  • Check if the element contains any spaces using the in keyword and the string " " as a parameter. If it does, set the flag variable to False and break out of the loop using the break keyword.
  • If the flag variable is still True after checking all elements in the current row, append the current row to the res list.
  • Print the filtered rows using the print() function and string concatenation.

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^2) (nested loop)
Auxiliary space: O(k) (where k is the length of the longest row)


Next Article

Similar Reads