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



In this article, we are going to find out how to remove empty strings from a list of strings in Python..

The first approach is by using the inbuilt method filter(). This method 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.

The built-in Python function filter() enables you to process an iterable and extract those elements that meet a specified condition. This action is frequently referred to as a filtering operation. You can use the filter() function to apply a filtering function to an iterable and create a new iterable that only contains the elements that match the given condition.

Example

In the program given below, we are taking a list of strings as input and we are removing the empty spaces using the filter() method and we are printing the modified list that is free of empty strings 

str_list = ["Tutorialspoint","","Welcomes","","Everyone",""]

print("The given list of strings is")
print(str_list)

print("Removing the empty spaces")
updated_list = list(filter(None, str_list))
print(updated_list)

Output

The output of the above example is as shown below −

The given list of strings is
['Tutorialspoint', '', 'Welcomes', '', 'Everyone', '']
Removing the empty spaces
['Tutorialspoint', 'Welcomes', 'Everyone']

Using join() and split() method

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 example given below, we are taking a list of strings as input and we are removing the empty strings using the join() method and split() method and we are printing the modified list that is free of empty strings 

str_list = ["Tutorialspoint","","Welcomes","","Everyone",""]

print("The given list of strings is")
print(str_list)

print("Removing the empty spaces")
updated_list = ' '.join(str_list).split()
print(updated_list)

Output

The output of the above example is given below −

The given list of strings is
['Tutorialspoint', '', 'Welcomes', '', 'Everyone', '']
Removing the empty spaces
['Tutorialspoint', 'Welcomes', 'Everyone']

Using remove() method

The third approach is the brute force approach i.e. by iterating over the list and then checking 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.

Example

In the example given below, we are taking a list of strings as input and we are removing empty strings using remove() method and loops and we are printing the modified list that is free of empty strings.

str_list = ["Tutorialspoint","","Welcomes","","Everyone",""]
print("The given list of strings is")
print(str_list)

print("Removing the empty spaces")
while ("" in str_list):
   str_list.remove("")
print(str_list)

Output

The output of the above example is as shown below−

The given list of strings is
['Tutorialspoint', '', 'Welcomes', '', 'Everyone', '']
Removing the empty spaces
['Tutorialspoint', 'Welcomes', 'Everyone']

Advertisements