Python | Remove empty strings from list of strings
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 all digits from a list of strings
- Python | Remove all strings from a list of tuples
- Python | Remove last character in list of strings
- Python | Remove trailing/leading special characters from strings list
- Python | Tokenizing strings in list of strings
- Python | Remove and print every third from list until it becomes empty
- Python | Remove empty tuples from a list
- Python | Remove trailing empty elements from given list
- Python | Filter list of strings based on the substring list
- Python | Merge list of tuple into list by joining the strings
- Python | Convert list of tuples to list of strings
- Python | Filter a list based on the given list of strings
- Python | Convert list of strings to list of tuples
- Python | How to sort a list of strings
- Python | Sort all sublists in given list of strings
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.



