Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Ways to remove multiple empty spaces from string List

Improve Article
Save Article
  • Last Updated : 26 Jan, 2023
Improve Article
Save Article

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




# Python3 code to demonstrate working of
# Remove multiple empty spaces from string List
# Using loop + strip()
 
# initializing list
test_list = ['gfg', ' ', ' ', 'is', '         ', 'best']
 
# printing original list
print("The original list is : " + str(test_list))
 
# Remove multiple empty spaces from string List
# Using loop + strip()
res = []
for ele in test_list:
    if ele.strip():
        res.append(ele)
     
# printing result
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']

  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




# Python3 code to demonstrate working of
# Remove multiple empty spaces from string List
# Using list comprehension + strip()
 
# initializing list
test_list = ['gfg', ' ', ' ', 'is', '         ', 'best']
 
# printing original list
print("The original list is : " + str(test_list))
 
# Remove multiple empty spaces from string List
# Using list comprehension + strip()
res = [ele for ele in test_list if ele.strip()]
     
# printing result
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']

Method #3 : Using find()

Python3




# Python3 code to demonstrate working of
# Remove multiple empty spaces from string List
# Using find()
 
# initializing list
test_list = ['gfg', ' ', ' ', 'is', '         ', 'best']
 
# printing original list
print("The original list is : " + str(test_list))
 
# Remove multiple empty spaces from string List
# Using find()
res = []
for ele in test_list:
    if ele.find(' ')==-1:
        res.append(ele)
     
# printing result
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




# Python3 code to demonstrate working of
# Remove multiple empty spaces from string List
# initializing list
test_list = ['gfg', ' ', ' ', 'is', '         ', 'best']
 
# printing original list
print("The original list is : " + str(test_list))
 
res = list(filter(lambda x: x[0].lower() != x[0].upper(), test_list))
 
# printing result
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']

Method #5:  Using itertools.filterfalse()

Python3




# Python3 code to demonstrate working of
# Remove multiple empty spaces from string List
# initializing list
import itertools
 
test_list = ['gfg', ' ', ' ', 'is', '         ', 'best']
 
# printing original list
print("The original list is : " + str(test_list))
 
res = list(itertools.filterfalse(lambda x: x[0].upper() == x[0].lower(), test_list))
 
# printing result
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)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!