In many scenarios, we encounter the issue of getting an empty string in a huge amount of data and handling that sometimes becomes a tedious task. Let’s discuss certain way outs to remove empty strings from list of strings.
Method #1 : Using remove()
This particular method is quite naive and not recommended to use, but is indeed a method to perform this task. remove() generally removes the first occurrence of empty string and we keep iterating this process until no empty string is found in list.
# Python3 code to demonstrate # removing empty strings # using remove() # initializing list test_list = ["", "GeeksforGeeks", "", "is", "best", ""] # Printing original list print ("Original list is : " + str(test_list)) # using remove() to # perform removal while("" in test_list) : test_list.remove("") # Printing modified list print ("Modified list is : " + str(test_list)) |
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', ''] Modified list is : ['GeeksforGeeks', 'is', 'best']
Method #2 : Using List Comprehension
More concise and better approach to remove all the empty strings, it just checks if the string is not empty and re-makes the list with all strings that are not empty.
# Python 3 code to demonstrate # removing empty strings # using list comprehension # initializing list test_list = ["", "GeeksforGeeks", "", "is", "best", ""] # Printing original list print ("Original list is : " + str(test_list)) # using list comprehension to # perform removal test_list = [i for i in test_list if i] # Printing modified list print ("Modified list is : " + str(test_list)) |
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', ''] Modified list is : ['GeeksforGeeks', 'is', 'best']
Method #3 : Using join() + split()
Combining both the join() and split() operations, this task can also be achieved. We first join all the strings so that empty space is removed, and then split it back to list so that new list made now has no empty string.
# Python3 code to demonstrate # removing empty strings # using join() + split() # initializing list test_list = ["", "GeeksforGeeks", "", "is", "best", ""] # Printing original list print ("Original list is : " + str(test_list)) # using join() + split() to # perform removal test_list = ' '.join(test_list).split() # Printing modified list print ("Modified list is : " + str(test_list)) |
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', ''] Modified list is : ['GeeksforGeeks', 'is', 'best']
Method #4 : Using filter()
Using filter() is the most elegant and fastest way to perform this task. This method is highly recommended because speed matters when we deal with large machine learning data set that may potentially contain empty string.
# Python 3 code to demonstrate # removing empty strings # using filter() # initializing list test_list = ["", "GeeksforGeeks", "", "is", "best", ""] # Printing original list print ("Original list is : " + str(test_list)) # using filter() to # perform removal test_list = list(filter(None, test_list)) # Printing modified list print ("Modified list is : " + str(test_list)) |
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', ''] Modified list is : ['GeeksforGeeks', 'is', 'best']
Recommended Posts:
- Python - Remove empty List from List
- Python | Remove empty tuples from a list
- Python | Remove and print every third from list until it becomes empty
- Python | Remove trailing empty elements from given list
- Python - Ways to remove multiple empty spaces from string List
- Python - Remove empty value types in dictionaries list
- Python - Merge consecutive empty Strings
- Python - Check if a list is empty or not
- Python | First Non-Empty String in list
- Python | Splitting list on empty string
- Python | Initialize list with empty dictionaries
- Python | Check if any String is empty in list
- Declare an empty List in Python
- Python | Remove last character in list of strings
- Python | Remove all digits from a list of strings
- Python | Remove Kth character from strings list
- Python | Remove trailing/leading special characters from strings list
- Python | Remove prefix strings from list
- Python | Remove given character from Strings list
- Python - Remove duplicate words from Strings in List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

