To remove empty strings from a list using Python, the easiest way is to use list comprehension.
list_of_strings = ["This","","is","a","list","","with","empty","","strings","."]
list_without_empty_strings = [x for x in list_of_strings if x != ""]
print(list_without_empty_strings)
#Output:
['This', 'is', 'a', 'list', 'with', 'empty', 'strings', '.']
You can also use the Python filter() function.
list_of_strings = ["This","","is","a","list","","with","empty","","strings","."]
list_without_empty_strings = list(filter(lambda x: x != "", list_of_strings))
print(list_without_empty_strings)
#Output:
['This', 'is', 'a', 'list', 'with', 'empty', 'strings', '.']
When working with lists of strings, it can be valuable to be able to easily filter and remove unwanted values from your list.
One such situation where you may want to remove values from a list is if you have a lot of empty strings in your list.
We can easily remove all empty strings from a list using Python with list comprehension. List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list.
We can use list comprehension to remove all instances of a particular value in a list easily.
Below is the code which will allow you to remove all instances of empty strings from a list using list comprehension in Python.
list_of_strings = ["This","","is","a","list","","with","empty","","strings","."]
list_without_empty_strings = [x for x in list_of_strings if x != ""]
print(list_without_empty_strings)
#Output:
['This', 'is', 'a', 'list', 'with', 'empty', 'strings', '.']
Removing Empty Strings from List with Python filter() Function
The Python filter() function is a built-in function that allows you to process an iterable and extract items that satisfy a given condition.
We can use the Python filter() function to extract all the items in a list of numbers which do not equal “” and remove the empty strings from a list.
Below is some example code showing you how to remove empty strings from a list using the filter() function.
list_of_strings = ["This","","is","a","list","","with","empty","","strings","."]
list_without_empty_strings = list(filter(lambda x: x != "", list_of_strings))
print(list_without_empty_strings)
#Output:
['This', 'is', 'a', 'list', 'with', 'empty', 'strings', '.']
Removing Any Value from List Using Python
In a very similar way, we can remove any value from a list using list comprehension.
For example, if we instead wanted to remove all zeros from a list, we could do that easily with list comprehension in Python by adjusting the code above.
list_of_numbers = [1,0,4,2,-4,0,0,3,0,-1,0]
list_without_zeros = [x for x in list_of_numbers if x != 0]
print(list_without_zeros)
#Output:
[1,4,2,-4,3,-1]
Another example would be if we have a list of strings, and we want to remove the word “whoa”, we can do that with list comprehension in Python.
list_of_strings = ["whoa","there","hey","there","whoa"]
filtered_list = [x for x in list_of_strings if x != "whoa"]
print(filtered_list)
#Output:
["there","hey","there"]
Hopefully this article has been useful for you to learn how to remove empty strings from a list in Python.