Remove Empty Strings from a List of Strings in Python



The Lists in Python are used to store collections of items such as numbers, strings. While working with the list of strings, it is common to find a empty strings in the list.

An empty string is nothing but a string value with zero length. Officially they are present as elements in the list, but does not hold any data and interfere in the operations like sorting, filtering. So, removing the empty strings is essential to ensure the list contains only valid values.

For achieving this, Python provides the several ways. Let's dive into the article to learn more about them.

Using Python filter() Function

The Python filter() function allows you to filter out elements from an iterable object based on the specified condition.

The first approach is by using the filter() function. This function takes input from a list of strings and removes empty strings and returns the updated list. It takes None as the first parameter because we are trying to remove empty spaces and the next parameter is the list of strings.

Syntax

Following is the syntax for Python filter() function -

filter(function, iterable)

Example

Let's look at the following example, where we are going to remove the empty strings using the filter() function.

str_list = ["Tutorialspoint","","Welcomes","","Everyone",""]
print("After removing empty Strings : ")
updated_list = list(filter(None, str_list))
print(updated_list)

The output of the above program is as follows -

After removing empty Strings : 
['Tutorialspoint', 'Welcomes', 'Everyone']

Using Python remove() Method

The Python remove() method is used to search for the given element in the list adn removes the matching element.

In this approach, we iterate over the list and then check each element if it is an empty string or not. If the string is empty, then that specific string is removed from the list using the remove() method of lists otherwise, we move on to the next string.

Syntax

Following is the syntax for Python remove() method -

list.remove(obj)

Example

Consider the following example, where we are going to remove the empty strings in the list using the remove() method.

str_list = ["Ciaz","","Cruze","","Cheron",""]
print("After removing empty Strings : ")
while ("" in str_list):
   str_list.remove("")
print(str_list)

The output of the above program is as follows -

After removing empty Strings : 
['Ciaz', 'Cruze', 'Cheron']

Using Python join() and split() method

The Python join() method takes all the elements in an iterable separated by the given separator and joins them into one string.

Syntax

Following is the syntax for Python join() method -

str.join(iterable)

The Python split() method is used to split all the words in a string separated by a specified separator.

Syntax

Following is the syntax for Python split() method -

str.split(separator, maxsplit)

The second approach is by using join() and split() methods. We will take the list of strings and split them at white space using the split() method by sending the whitespace as parameter, then we will join all of them using the join() method.

Example

In the following example, we are going to check whether the string is empty or not by using the equality comparison.

str_list = ["HTML","","Python","","Java",""]
print("After removing empty strings : ")
updated_list = ' '.join(str_list).split()
print(updated_list)

The following is the output of the above program -

After removing empty strings : 
['HTML', 'Python', 'Java']
Updated on: 2025-04-21T15:51:52+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements