Python – Ways to remove multiple empty spaces from string List

30 Jun, 2022
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 number of spaces. This has application in many domains. Lets 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 single space and then it can be tested for NULL value. Returns True if string is 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 listtest_list = ['gfg', '   ', ' ', 'is', '            ', 'best'] # printing original listprint("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 resultprint("List after filtering non-empty strings : " + str(res))
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 above functions can also be used to perform this task. In this, we employ one-liner approach to perform this task instead of using loop. 
Python3
# Python3 code to demonstrate working of# Remove multiple empty spaces from string List# Using list comprehension + strip() # initializing listtest_list = ['gfg', '   ', ' ', 'is', '            ', 'best'] # printing original listprint("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 resultprint("List after filtering non-empty strings : " + str(res))
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 listtest_list = ['gfg', ' ', ' ', 'is', '         ', 'best'] # printing original listprint("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 resultprint("List after filtering non-empty strings : " + str(res))
The original list is : ['gfg', ' ', ' ', 'is', '\t\t ', 'best']
List after filtering non-empty strings : ['gfg', 'is', 'best']
kogantibhavya
Python list-programs
Python string-programs
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
Python program to convert a list to string
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Convert a list to dictionary
Python Program for Fibonacci numbers