How To Remove Empty Strings From A List Of Strings In Python

This article will show you how to remove empty Strings from a list of Strings in Python. It will help you clean and remove unnecessary data from the list. Let’s learn more about it with the explanation and examples below.

Remove Empty Strings From A List Of Strings In Python

In this title, we will share with you how to remove empty strings from a list of Strings in Python in four ways: 

  • Using the list comprehension
  • Using the filter() function.
  • Using the loop combined with the remove() function.
  • Using the join() function with the split() function.

Let’s approach these ways below.

Using the list comprehension

In this solution, we will use list comprehension to create a new list based on the original list and its condition.

Look at the example below to learn more about this solution.

# Create a list of Strings
oldList = ['LeanShareIT', '', 'GeeksForGeeks', 'Python', '', 'String', 'List', 'StackOverFlow', '', '']

print('The original list: ' + str(oldList))

# Remove the empty Strings in the old list
newList = [x for x in oldList if x != '']
print('The new list: ' + str(newList))

Output

The original list: ['LeanShareIT', '', 'GeeksForGeeks', 'Python', '', 'String', 'List', 'StackOverFlow', '', '']
The new list: ['LeanShareIT', 'GeeksForGeeks', 'Python', 'String', 'List', 'StackOverFlow']

Using the filter() function

In this solution, we will use the filter() function to return a new list that does not contain the empty Strings.

Look at the example below to learn how to perform it.

# Create a list of Strings
oldList = ['LeanShareIT', '', 'GeeksForGeeks', 'Python', '', 'String', 'List', 'StackOverFlow', '', '']
print('The original list: ' + str(oldList))

# Remove the empty Strings in the old list
newList = list(filter(None, oldList))
print('The new list: ' + str(newList))

Output

The original list: ['LeanShareIT', '', 'GeeksForGeeks', 'Python', '', 'String', 'List', 'StackOverFlow', '', '']
The new list: ['LeanShareIT', 'GeeksForGeeks', 'Python', 'String', 'List', 'StackOverFlow']

Using the for loop combined with the remove() function

You can remove empty strings from a list of Strings in Python by removing the empty String until the list does not contain any empty Strings.

Look at the example below.

# Create a list of Strings
oldList = ['LeanShareIT', '', 'GeeksForGeeks', 'Python', '', 'String', 'List', 'StackOverFlow', '', '']
print('The original list: ' + str(oldList))

# Remove the empty Strings in the old list
while '' in oldList:
    oldList.remove('')

print('The new list:' + str(oldList))

Output

The original list: ['LeanShareIT', '', 'GeeksForGeeks', 'Python', '', 'String', 'List', 'StackOverFlow', '', '']
The new list: ['LeanShareIT', 'GeeksForGeeks', 'Python', 'String', 'List', 'StackOverFlow']

Using the for join() function combined with the split() function

Firstly, you join all elements of the old String. Then, you split it by the split() function, and the result will be a list that does not contain any empty Strings.

Note that this solution is only suitable with a list with no more than 1-word elements.

Look at the example below.

# Create a list of Strings
oldList = ['LeanShareIT', '', 'GeeksForGeeks', 'Python', '', 'String', 'List', 'StackOverFlow', '', '']
print('The original list: ' + str(oldList))

# Remove the empty Strings in the old list
newList = ' '.join(oldList).split()
print('The new list:' + str(newList))

Output

The original list: ['LeanShareIT', '', 'GeeksForGeeks', 'Python', '', 'String', 'List', 'StackOverFlow', '', '']
The new list: ['LeanShareIT', 'GeeksForGeeks', 'Python', 'String', 'List', 'StackOverFlow']

Summary

We have shown you how to remove empty strings from a list of Strings in Python in 4 ways. But it would be best to do that with the list comprehension because it is similar and easy to perform. We hope this tutorial is helpful to you. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *