Select Page

How to Remove Empty Strings from a List of Strings in Python

by | Programming, Python, Tips

If you need to remove empty strings from a list of strings in Python, there are several ways to do so. You can use the built-in methods remove(), join() and split() together, and filter(). You can also remove empty strings using list comprehension.

This tutorial will go through how to remove empty strings from a list using the different approaches with code examples.


Remove Empty Strings Using List Comprehension

List comprehension is a concise way to create a new list from the values of an existing list. We can use list comprehension to remove empty strings from a list. The syntax for list comprehension is:

new_list = [expression for item in iterable if condition == True]

List comprehension returns a new list and leaves the old list unchanged. Let’s look at an example:

test_list = ["", "Python", "is", "really", "", "fun", "", "to", "learn", ""]

print("Original list is:  " + str(test_list))

mod_list = [i for i in test_list if i]

print("Modified list is: " + str(mod_list))

The list comprehension checks if the string is not empty with the if i condition and returns a list with all non-empty strings. Let’s run the code to see the result:

Original list is:  ['', 'Python', 'is', 'really', '', 'fun', '', 'to', 'learn', '']
Modified list is: ['Python', 'is', 'really', 'fun', 'to', 'learn']

Remove Empty Strings Using remove()

The built-in method remove() removes the first occurrence of the element within the object. The remove() method is not the most efficient approach because it only removes the first occurrence. We have to put the call to the remove() method in a loop to remove all empty strings. Let’s look at an example:

test_list = ["", "Python", "is", "really", "", "fun", "", "to", "learn", ""]

print("Original list is:  " + str(test_list))

while ("" in test_list):
    test_list.remove("")

print("Modified list is:  " + str(test_list))

We use a while loop to check the list for an empty string and call the remove() function if an empty string is present. The remove() function modifies the list in place. Let’s run the code to see the result:

Original list is:  ['', 'Python', 'is', 'really', '', 'fun', '', 'to', 'learn', '']
Modified list is:  ['Python', 'is', 'really', 'fun', 'to', 'learn']

Remove Empty Strings Using join() and split()

The join() method takes all items in the list and joins them into one string. The split() method splits a string into a list. We can combine join() and split() to join all the strings to remove empty space, then split the string back to a list with no empty strings. Let’s look at an example:

test_list = ["", "Python", "is", "really", "", "fun", "", "to", "learn", ""]

print("Original list is:  " + str(test_list))

mod_list = ' '.join(test_list).split()

print("Modified list is:  " + str(mod_list))

The join() method needs a string as a separator. In this example, we use white space as the separator. The split method has a default separator of whitespace. Let’s run the code to see the result:

Original list is:  ['', 'Python', 'is', 'really', '', 'fun', '', 'to', 'learn', '']
Modified list is:  ['Python', 'is', 'really', 'fun', 'to', 'learn']

Remove Empty Strings Using filter()

The filter() function is preferred to remove empty strings from a list of strings. The syntax of filter() function is

filter(function_name, iterable)

The parameter function_name is the function to run for each item in the iterable. The parameter iterable is the object we want to filter. Let’s look at an example:

test_list = ["", "Python", "is", "really", "", "fun", "", "to", "learn", ""]

print("Original list is:  " + str(test_list))

mod_list = list(filter(None, test_list))

print("Modified list is:  " + str(mod_list))

In the above code, we pass None as the function and the list we want to filter as the iterable. To convert the output from the filter() method to a list, we must pass the output to the list() method. Let’s run the code to get the result:

Original list is:  ['', 'Python', 'is', 'really', '', 'fun', '', 'to', 'learn', '']
Modified list is:  ['Python', 'is', 'really', 'fun', 'to', 'learn']

Summary

Congratulations on reading to the end of this tutorial! You have gone through several ways to remove empty strings from a list of strings. The filter() method is the most efficient way to remove empty strings, and this particular method is suitable for cleaning large datasets containing empty strings.

For further reading on list manipulation, go to the article: How to Convert a List to a String in Python.

Go to the online courses page on Python to learn more about coding in Python for data science and machine learning.

Have fun and happy researching!