Python – Ways to remove multiple empty spaces from string List
Last Updated :
21 Mar, 2023
Sometimes, while working with Python strings, we have a problem in which we need to perform the removal of empty spaces in Strings. The problem of filtering a single space is easier. But sometimes we are unaware of the number of spaces. This has applications in many domains. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using loop + strip() This is a way in which we can perform this task. In this, we strip the strings using strip(), it reduces to a single space, and then it can be tested for a NULL value. Returns True if the string is a single space and hence helps in filtering.
Python3
test_list = [ 'gfg' , ' ' , ' ' , 'is' , ' ' , 'best' ]
print ( "The original list is : " + str (test_list))
res = []
for ele in test_list:
if ele.strip():
res.append(ele)
print ( "List after filtering non-empty strings : " + str (res))
|
Output :
The original list is : ['gfg', ' ', ' ', 'is', ' ', 'best']
List after filtering non-empty strings : ['gfg', 'is', 'best']
Time Complexity: O(n) where n is the total number of values in the list “test_list”.
Auxiliary Space: O(n) where n is the total number of values in the list “test_list”.
Method #2: Using list comprehension + strip() The combination of the above functions can also be used to perform this task. In this, we employ a one-liner approach to perform this task instead of using the loop.
Python3
test_list = [ 'gfg' , ' ' , ' ' , 'is' , ' ' , 'best' ]
print ( "The original list is : " + str (test_list))
res = [ele for ele in test_list if ele.strip()]
print ( "List after filtering non-empty strings : " + str (res))
|
Output :
The original list is : ['gfg', ' ', ' ', 'is', ' ', 'best']
List after filtering non-empty strings : ['gfg', 'is', 'best']
Time Complexity: O(n)
Auxiliary Space: O(n), where n is length of list.
Method #3 : Using find()
Python3
test_list = [ 'gfg' , ' ' , ' ' , 'is' , ' ' , 'best' ]
print ( "The original list is : " + str (test_list))
res = []
for ele in test_list:
if ele.find( ' ' ) = = - 1 :
res.append(ele)
print ( "List after filtering non-empty strings : " + str (res))
|
Output
The original list is : ['gfg', ' ', ' ', 'is', '\t\t ', 'best']
List after filtering non-empty strings : ['gfg', 'is', 'best']
The Time and Space Complexity for all the methods are the same:
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4: Using lambda function
Python3
test_list = [ 'gfg' , ' ' , ' ' , 'is' , ' ' , 'best' ]
print ( "The original list is : " + str (test_list))
res = list ( filter ( lambda x: x[ 0 ].lower() ! = x[ 0 ].upper(), test_list))
print ( "List after filtering non-empty strings : " + str (res))
|
Output
The original list is : ['gfg', ' ', ' ', 'is', ' ', 'best']
List after filtering non-empty strings : ['gfg', 'is', 'best']
Time complexity: O(n), where n is the length of the test_list.
Auxiliary Space: O(n), extra space of size n is required
Method #5: Using itertools.filterfalse()
Python3
import itertools
test_list = [ 'gfg' , ' ' , ' ' , 'is' , ' ' , 'best' ]
print ( "The original list is : " + str (test_list))
res = list (itertools.filterfalse( lambda x: x[ 0 ].upper() = = x[ 0 ].lower(), test_list))
print ( "List after filtering non-empty strings : " + str (res))
|
Output
The original list is : ['gfg', ' ', ' ', 'is', ' ', 'best']
List after filtering non-empty strings : ['gfg', 'is', 'best']
Time Complexity: O(n)
Auxiliary Space: O(n)
Approach 6: Using str.isspace()
Python3
test_list = [ 'gfg' , ' ' , ' ' , 'is' , ' ' , 'best' ]
print ( "The original list is : " + str (test_list))
res = [ele for ele in test_list if not ele.isspace()]
print ( "List after filtering non-empty strings : " + str (res))
|
Output
The original list is : ['gfg', ' ', ' ', 'is', ' ', 'best']
List after filtering non-empty strings : ['gfg', 'is', 'best']
Time Complexity: O(n)
Auxiliary Space: O(n)
Explanation:
In this approach, we use the str.isspace() method which returns True if all the characters in the string are whitespaces and False otherwise.
We loop through the list and use the str.isspace() method to check if the string only consists of whitespaces or not.
If not, then we append the string to the result list. Finally, we return the result list which contains all the non-empty strings from the original list.
Approach #7:Using regex.findall() method
Python3
import re
test_list = [ 'gfg' , ' ' , ' ' , 'is' , ' ' , 'best' ]
print ( "The original list is : " + str (test_list))
string = ''.join(test_list)
res = re.findall(r '[a-zA-Z]+' , string)
print ( "List after filtering non-empty strings : " + str (res))
|
Output
The original list is : ['gfg', ' ', ' ', 'is', ' ', 'best']
List after filtering non-empty strings : ['gfg', 'is', 'best']
Time Complexity: O(n)
Auxiliary Space: O(n)
Please Login to comment...