Python – Filter rows without Space Strings
Last Updated :
26 Apr, 2023
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
import re
test_list = [[ "gfg is" , "best" ], [ "gfg" , "good" ],
[ "gfg is cool" ], [ "love" , "gfg" ]]
print ( "The original list is : " + str (test_list))
res = [row for row in test_list if not any (
bool (re.search(r "\s" , ele)) for ele in row)]
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
import re
test_list = [[ "gfg is" , "best" ], [ "gfg" , "good" ],
[ "gfg is cool" ], [ "love" , "gfg" ]]
print ( "The original list is : " + str (test_list))
res = list ( filter ( lambda row: not any ( bool (re.search(r "\s" , ele))
for ele in row), test_list))
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
test_list = [[ "gfg is" , "best" ], [ "gfg" , "good" ],
[ "gfg is cool" ], [ "love" , "gfg" ]]
print ( "The original list is : " + str (test_list))
res = []
for i in test_list:
a = "".join(i)
if (a.find( " " ) = = - 1 ):
res.append(i)
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
import itertools
import re
test_list = [[ "gfg is" , "best" ], [ "gfg" , "good" ],
[ "gfg is cool" ], [ "love" , "gfg" ]]
print ( "The original list is : " + str (test_list))
res = list (itertools.filterfalse( lambda row: any ( bool (re.search(r "\s" , ele))
for ele in row), test_list))
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
test_list = [[ "gfg is" , "best" ], [ "gfg" , "good" ],
[ "gfg is cool" ], [ "love" , "gfg" ]]
print ( "The original list is : " + str (test_list))
res = [row for row in test_list if all (ele.split() = = [ele] for ele in row)]
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)
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.
Python3
test_list = [[ "gfg is" , "best" ], [ "gfg" , "good" ],
[ "gfg is cool" ], [ "love" , "gfg" ]]
print ( "The original list is : " + str (test_list))
res = []
for row in test_list:
flag = True
for ele in row:
if " " in ele:
flag = False
break
if flag:
res.append(row)
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^2) (nested loop)
Auxiliary space: O(k) (where k is the length of the longest row)
Similar Reads
Python - Filter Strings within ASCII range
Given ASCII or alphabetical range, filter strings are found in a particular range. Input : test_list = ["gfg", "is", "best", "for", "geeks"], strt_asc, end_asc = 105, 115 Output : ['is'] Explanation : i has 105, and s has 115, which is in range ASCII values.Input : test_list = ["gfg", "is", "best",
3 min read
Python | Remove unwanted spaces from string
Sometimes, while working with strings, we may have situations in which we might have more than 1 spaces between intermediate words in strings that are mostly unwanted. This type of situation can occur in web development and often needs rectification. Let's discuss certain ways in which this task can
4 min read
Python - Filter Sorted Rows
Given a Matrix, extract rows that are sorted, either by ascending or descending. Input : test_list = [[3, 6, 8, 10], [1, 8, 2, 4, 3], [8, 5, 3, 2], [1, 4, 5, 3]] Output : [[3, 6, 8, 10], [8, 5, 3, 2]] Explanation : Both lists are ordered, 1st ascending, second descending. Input : test_list = [[3, 6,
3 min read
Python - Filter above Threshold size Strings
Sometimes, while working with huge amounts of data, we can have a problem in which we need to extract just specific-sized strings above a minimum threshold. This kind of problem can occur during validation cases across many domains. Letâs discuss certain ways to handle this in Python strings list. M
5 min read
Python | Slice String from Tuple ranges
Sometimes, while working with data, we can have a problem in which we need to perform the removal from strings depending on specified substring ranges. Let's discuss certain ways in which this task can be performed. Method #1: Using loop + list slicing: This is the brute force task to perform this t
3 min read
Remove spaces from a string in Python
Removing spaces from a string is a common task in Python that can be solved in multiple ways. For example, if we have a string like " g f g ", we might want the output to be "gfg" by removing all the spaces. Let's look at different methods to do so: Using replace() methodTo remove all spaces from a
2 min read
Python - Extract rows with Even length strings
In this article, we have a given Matrix, and extract rows that are of even lengths. Input : test_list = [["gfg", "is", "best"], ["best", "good", "geek"], ["is", "better"], ["for", "cs"]] Output : [['best', 'good', 'geek'], ['is', 'better']] Explanation : All strings are of even length.Input : test_l
8 min read
Python - Filter Rows with Range Elements
Given a Matrix, filter all the rows which contain all elements in the given number range. Input : test_list = [[3, 2, 4, 5, 10], [3, 2, 5, 19], [2, 5, 10], [2, 3, 4, 5, 6, 7]], i, j = 2, 5 Output : [[3, 2, 4, 5, 10], [2, 3, 4, 5, 6, 7]] Explanation : 2, 3, 4, 5 all are present in above rows. Input :
5 min read
Python - Ways to remove multiple empty spaces from string List
We are given a list of strings li =["Hello world", " Python is great ", " Extra spaces here "] we need to remove all extra spaces from the strings so that the output becomes ["Hello world", "Python is great' , "Extra spaces here"] .This can be done using methods like split() and join(), regular expr
3 min read
Python - Remove keys with substring values
Sometimes, we need to remove keys whose values contain a specific substring. For example, consider the dictionary d = {'name1': 'hello world', 'name2': 'python code', 'name3': 'world peace'}. If we want to remove keys where the value contains the substring 'world', the resulting dictionary should ex
3 min read