Python | Remove empty strings from list of strings

07 Jul, 2022
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 use, but is indeed a method to perform this task. remove() generally removes the first occurrence of an empty string and we keep iterating this process until no empty string is found in list. 
Python3
# Python3 code to demonstrate# removing empty strings# using remove() # initializing listtest_list = ["", "GeeksforGeeks", "", "is", "best", ""] # Printing original listprint ("Original list is : " + str(test_list)) # using remove() to# perform removalwhile("" in test_list) :    test_list.remove("")     # Printing modified listprint ("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. 
Python3
# Python 3 code to demonstrate# removing empty strings# using list comprehension # initializing listtest_list = ["", "GeeksforGeeks", "", "is", "best", ""] # Printing original listprint ("Original list is : " + str(test_list)) # using list comprehension to# perform removaltest_list = [i for i in test_list if i]     # Printing modified listprint ("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 the new list made now has no empty string.  
Python3
# Python3 code to demonstrate# removing empty strings# using join() +  split() # initializing listtest_list = ["", "GeeksforGeeks", "", "is", "best", ""] # Printing original listprint ("Original list is : " + str(test_list)) # using join() +  split() to# perform removaltest_list = ' '.join(test_list).split()     # Printing modified listprint ("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. 
Python3
# Python 3 code to demonstrate# removing empty strings# using filter() # initializing listtest_list = ["", "GeeksforGeeks", "", "is", "best", ""] # Printing original listprint ("Original list is : " + str(test_list)) # using filter() to# perform removaltest_list = list(filter(None, test_list))     # Printing modified listprint ("Modified list is : " + str(test_list))
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
Method #5 : Using len() method.
Python3
# Python3 code to demonstrate# removing empty strings  # initializing listtest_list = ["", "GeeksforGeeks", "", "is", "best", ""] # Printing original listprint ("Original list is : " + str(test_list)) for i in test_list:    if(len(i)==0):        test_list.remove(i)     # Printing modified listprint ("Modified list is : " + str(test_list))
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
kogantibhavya
Python list-programs
Python string-programs
python-list
Python
python-list
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()
How to Install PIP on Windows ?
*args and **kwargs in Python
Iterate over a list in Python
Python Classes and Objects
Convert integer to string in Python