How to remove empty strings from a list of strings in Python?

In Python, lists are a versatile data structure used to store collections of items. Sometimes, when working with lists of strings, you may encounter situations where the list contains empty strings. Empty strings are strings that have no characters in them, represented by '' or "". These empty strings can clutter your data and may need to be removed to streamline your code or data processing. In this article, we'll explore various methods to remove empty strings from a list of strings in Python.

Method 1: Using a List Comprehension

One of the most common and efficient ways to remove empty strings from a list is by using a list comprehension. List comprehensions provide a concise way to create lists and can be used to filter out empty strings. Here's how you can do it:

Output

 
['hello', 'world', 'python']

In this code snippet, we iterate over each string in list_of_strings and only include it in filtered_list if the string evaluates to True (i.e., it is not an empty string).

Method 2: Using the filter() Function

The filter() function in Python can also be used to remove empty strings from a list. The filter() function takes two arguments: a function and an iterable. It returns an iterator that yields items from the iterable for which the function returns True. Here's how you can use filter() to remove empty strings:

Output

['hello', 'world', 'python']

In this example, the None function is used as the first argument to filter(), which removes all items from list_of_strings that evaluate to False. Since empty strings evaluate to False, they are removed from the list.

Method 3: Using a For Loop

Another approach to removing empty strings from a list is by using a for loop. While this method is more verbose compared to list comprehensions or filter(), it provides a clear and explicit way to achieve the desired result. Here's how you can do it:

Output

 
['hello', 'world', 'python']

In this example, we iterate over each string in list_of_strings and only append it to filtered_list if it is not an empty string.

Method 4: Using the remove() Method

If you want to remove empty strings from a list in place (i.e., without creating a new list), you can use the remove() method. The remove() method removes the first occurrence of a specified value from the list. Here's how you can use it:

Output

 
['hello', 'world', 'python']

In this example, we iterate over the list in reverse order using reversed() to avoid index issues when removing items. We then use the remove() method to remove empty strings from the list.

Method 5: Using a While Loop

Another approach to removing empty strings from a list in place is by using a while loop. This method is similar to using a for loop but provides more control over the iteration process. Here's how you can do it:

Output

 
['hello', 'world', 'python']

In this example, we use a while loop to iterate over the list. If an empty string is encountered, it is removed from the list using the del statement. The index is only incremented if a non-empty string is encountered, ensuring that no items are skipped.

Conclusion

In this article, we explored various methods to remove empty strings from a list of strings in Python. We discussed using list comprehensions, the filter() function, for loops, the remove() method, and while loops. Each method has its own advantages and can be used based on the specific requirements of your program. By removing empty strings, you can clean up your data and make it more manageable for further processing.