Remove Empty Strings from a List of Strings in Python

In this Python tutorial, we will learn how to remove empty strings from a list of strings.

Table Of Contents

Remove empty strings from a list of strings using remove()

In Python, list class has a function remove(). It accepts an element as an argument and removes the first occurrence of given element from the list. We will remove all empty strings from a list using the remove() method.

Here we will run a while loop, till the list has any empty string. During iteration we will keep removing an empty string from the list using remove() function. Finally, we will display the final list.

Example:

# Consider the list
listOfStrs = ["Welcome", "", "to", "", "thisPointer", ""]

# Actual List
print("Existing list-",listOfStrs)

# Remove empty strings from the above list
while "" in listOfStrs:
    listOfStrs.remove("")

# Final List
print("List after removing empty strings: ",listOfStrs)

Output:

Existing list- ['Welcome', '', 'to', '', 'thisPointer', '']
List after removing empty strings:  ['Welcome', 'to', 'thisPointer']

In this example, list had three empty strings. We removed them using the remove() method. Our final list is free from empty strings.

Remove empty strings from a list of strings using List Comprehension

In this method, we will iterate over the list using list comprehension and will select only non empty strings from the list.

Syntax:

[elem for elem in listOfStrs if elem]

Here, listOfStrs is the list with strings. We used if condition within the list comprehension to skip empty strings. List comprehension will give us a new list with non empty strings only.

Example:

In this example, we are having a list that has some empty strings. We will remove those empty strings from the list using list comprehension.

listOfStrs = ["Welcome","","to","","thisPointer",""]

# Actual List
print("Existing list-", listOfStrs)

# Remove empty strings from list
listOfStrs = [elem for elem in listOfStrs if elem]

# Remove empty strings from the above list
print("List after removing empty strings: ", listOfStrs)

Output:

Existing list- ['Welcome', '', 'to', '', 'thisPointer', '']
List after removing empty strings:  ['Welcome', 'to', 'thisPointer']

In the list there were three empty strings. We selected only non-empty strings into a new list using the List Comprehension and assigned it back to original variable. Now the final list is free from empty strings.

Remove empty strings from a list of strings using join()

In this method, first join the strings in the list using the join() function of the string class. Use single whitespace string as a delimeter while joining the strings in list. Then again split the string into a list using the split() function. By default split() will use whitespace as seperator. Now the returned list will have no empty strings in it.

Example:

In this example, we have a list that has some empty strings. Now we will remove those empty strings from the list.

listOfStrs = ["Welcome","","to","","thisPointer",""]

# Actual List
print("Existing list-", listOfStrs)

# Join strings in a list and again split them
# It will remove empty strings from list
listOfStrs = ' '.join(listOfStrs).split()

# Remove empty strings from the above list
print("List after removing empty strings: ", listOfStrs)

Output:

Existing list- ['Welcome', '', 'to', '', 'thisPointer', '']
List after removing empty strings:  ['Welcome', 'to', 'thisPointer']

In the listOfStrs, there are three empty strings. select only non-empty strings into a new list using the join() method. Now our final list is free from empty strings.

Remove empty strings from a list of strings using filter()

The filter() method can filter/remove the empty strings from the list. Pass None as the first parameter that represents the empty string.

Syntax:

list(filter(None, listOfStrs))

It takes two parameters

  1. None specifies an empty string.
  2. listOfStrs is the input list that has some empty strings.

It filters the empty strings from list and returns a list of non empty strings only.

Example:

In this example, we have a list with some empty strings. Now we will remove those empty strings from the list and place all the non-empty string elements in a new list through the list() method.

# Consider the list
listOfStrs = ["Welcome","","to","","thisPointer",""]

# Actual List
print("Existing list-", listOfStrs)

# It will filter/remove empty strings from list
listOfStrs = list(filter(None, listOfStrs))

# Remove empty strings from the above list
print("List after removing empty strings: ", listOfStrs)

Output:

Existing list- ['Welcome', '', 'to', '', 'thisPointer', '']
List after removing empty strings:  ['Welcome', 'to', 'thisPointer']

In the listOfStrs, there were three empty strings. The filter() function removed these three empty strings and returned the remaining strings in a list. We assigned this new list to the original list variable. This way our list is now free from empty strings.

Remove empty strings from a list of strings using len() with remove()

Empty strings are of 0 length. So, we can check the length of all strings in the list and remove the strings with length 0. For that, iterate over a copy of list of strings and for each element check if its length is 0 or not. If yes, then remove that string from the original list using the remove() method of the list class.

Syntax:

for elem in listOfStrs[:]:
    if(len(elem)==0):
        listOfStrs.remove(elem)

If you want remove elements from a list during iteration, then you should make a copy of the list first and then iterate over it. Because during iteration if we try to remove elements from the list, either iteration will fail or it might result in an unexpected behaviour. So, iterate over a copy of list and remove elements from original list.

The list.remove() function accepts an element as argument and removes that element from the list. So, above syntax will remove all the empty strings from the list.

Example:

In this example, we are having a list that has some empty strings. Now we will remove those empty strings from the list.

listOfStrs=["Welcome","","to","","thisPointer",""]

# Actual List
print("Existing list-",listOfStrs)

# Remove empty strings from the above list
for ele in listOfStrs[:]:
    if(len(ele)==0):
        listOfStrs.remove(ele)

# Final list
print("List after removing empty strings: ",listOfStrs)

Output:

Existing list- ['Welcome', '', 'to', '', 'thisPointer', '']
List after removing empty strings:  ['Welcome', 'to', 'thisPointer']

The List listOfStrs had three empty strings. The remove() method removed all the strings with length 0. Now you can see the final list which is free from empty strings.

Summary

We discussed five ways to remove empty strings from a list of strings. Based on the requirement, you can use any of the above methods to remove the empty strings from the existing python list. Thanks.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top