How to Remove Empty Strings from a List of Strings in Python [8 Methods]

In this Python tutorial, you’ll learn how to remove empty strings from a list in Python. I will demonstrate different methods with descriptive examples to remove empty strings from a list of strings in Python.

Removing empty strings in Python is a common task when working with text data, as it helps clean and filter the list of strings.

Keep reading this article to learn various methods to remove empty strings from a list of strings in Python.

  • Using list comprehension
  • Using remove() function
  • Using len() with remove()
  • Using filter() function
  • Using filter() and lambda() methods
  • Using split() and join() methods
  • Using a for-loop
  • Using enumerate() function

Python program to remove empty strings from a list of strings using list comprehension

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

List comprehension in Python offers a shorter syntax when we want to create a new list based on the values of an existing list in Python.

This is the complete program to remove empty strings from a list of strings in Python using list comprehension.

list_with_empty_strings = ['California', 'Texas', '', 'Florida', '', 'Alaska', 'Hawaii', '']
states = [x for x in list_with_empty_strings if x != '']

print(states) 

This list comprehension creates a new list containing only the elements from list_with_empty_strings that are not empty strings. It will remove empty strings from the original list in Python.

 [x for x in list_with_empty_strings if x != '']

Output:

['California', 'Texas', 'Florida', 'Alaska', 'Hawaii']

You can also refer to the screenshot below:

Python program to remove empty strings from a list of strings

Remove empty strings from a list of strings in Python using the remove() function

In Python, the remove() function accepts an element as an argument and removes the first occurrence of the given element from the list.

READ:  Python String Concatenation [8 Methods]

Below is the full code to remove empty strings from a list of strings in Python using the remove() function.

names = ["", "Jonny", "", "Louis", "", "Marsh","Alex"]
print("Original List: ",names)
while("" in names):names.remove("")

print("After removing empty strings from the list: ", names)

Output:

Original List:  ['', 'Jonny', '', 'Louis', '', 'Marsh', 'Alex']
After removing empty strings from the list:  ['Jonny', 'Louis', 'Marsh', 'Alex']

You can also refer to the image below to see the output of Python code used to remove empty strings from a list using the remove() function.

How to remove strings from a list of strings in Python using the remove() function

How to remove strings from a list of strings using the len() with remove()

This is another way to remove strings from a list of strings using the len() with the remove() function in Python. Empty strings are of 0 lengths. So, we can check the length of all strings in the list and remove the strings with length 0.

For that, iterate over a sequence of a list of strings, and for each element, check if its length is 0 or not. If yes, remove that string from the Python list using the remove() function.

This is Python’s full code to remove strings from a list of strings using the len() and remove() methods.

car_list=["BMW","","Audi","","Chevrolet",""]
print("Existing list-",car_list)

for name in car_list[:]:
    if(len(name)==0):
        car_list.remove(name)
print("List after removing empty strings: ",car_list)

Output:

Existing list- ['BMW', '', 'Audi', '', 'Chevrolet', '']
List after removing empty strings:  ['BMW', 'Audi', 'Chevrolet']

Refer to the below image to see the output of the Python program.

Python Remove Empty Strings from List

Python program to remove strings from a list of strings using filter() function

The filter() function returns the given sequence with the help of a function in Python that tests each element in the sequence to be True or False.

READ:  How to Add Elements in List in Python Using For Loop [5 use cases]

Here is Python’s full code to remove strings from a list of strings using the filter() function.

list_with_empty_cities = ['New York', 'Chicago', '', '', '', 'Dallas', 'Phoenix', '']
new_list_of_cities = list(filter(None, list_with_empty_cities))

print(new_list_of_cities)

Output:

['New York', 'Chicago', 'Dallas', 'Phoenix']

You can refer to the below screenshot, it gave the output of the Python program.

Remove strings from a list of string using filter()

How to remove empty strings from a list of strings in Python using filter() and lambda()

In Python, the lambda function can take any number of arguments but only have one expression.

This source code removes strings from a list of strings in Python using filter() with lambda function.

empty_string_list = ['Sailing', '', 'Kite Sufring', 'Scuba Diving', '', 'Snorkeling', '']
list_of_water_sports = list(filter(lambda x: x != '', empty_string_list))
print(list_of_water_sports)

Here, I am filtering out empty strings from the list named empty_string_list.

The lambda function in Python checks, if each element x is not equal to an empty string, and only elements that satisfy this condition, are included in the new list.

list_of_water_sports = list(filter(lambda x: x != '', empty_string_list))

Output:

['Sailing', 'Kite Sufring', 'Scuba Diving', 'Snorkeling']

You can see the code output to remove empty strings from a list of strings in Python.

remove strings from a list of strings in Python using filter() and lambda()

Remove strings from a list of strings using join() and split()

This is another way to remove strings from a list of strings using join() and split(). Using this join() and split(), it will first join all the strings so that space is removed and then split it back to the list so that the new list made now has no empty string in Python.

Here is the complete code to remove empty strings from a list of strings in Python using the join() and split() methods.

USA_lake_with_emptylist = ["", "Michigan", "", "Ontario", "Huron", "", "Erie "]
print("Original list is : " + str(USA_lake_with_emptylist))

USA_lake_with_emptylist = ' '.join(USA_lake_with_emptylist).split()
print("Modified list is : " + str(USA_lake_with_emptylist))

Here, it will split into a list of substrings using spaces as separators with a join() function and then split the resulting string back into a list using spaces as separators with the .split() method.

USA_lake_with_emptylist = ' '.join(USA_lake_with_emptylist).split()
print("Modified list is : " + str(USA_lake_with_emptylist))

Output:

Original list is : ['', 'Michigan', '', 'Ontario', 'Huron', '', 'Erie ']
Modified list is : ['Michigan', 'Ontario', 'Huron', 'Erie']

Refer to the screenshot below to see the output of the above Python program.

How to remove empty strings from a list of strings in Python using join() and split()

Python program to remove empty strings from a list of strings using a for loop

You can also remove empty strings from a list of strings in Python using a for loop, as we can iterate over the string characters.

READ:  How to Fix Invalid Syntax in Python

Below is the program to remove empty strings from a list of strings in Python using a for loop.

empty_list_of_strings = ["GAP", "", "", "Peter England", "Dior", "Louis Vuittion"]
updated_list = []
for element in empty_list_of_strings:
    if element != '':
        updated_list.append(element)
 
print(updated_list)

It iterates over each element in the given list using a for loop in Python. If the element is not an empty string, append it to updated_list.

for element in empty_list_of_strings:
    if element != '':
        updated_list.append(element)

Output:

['GAP', 'Peter England', 'Dior', 'Louis Vuittion']

You can refer to the below image to see the output of the above Python code.

Python program to remove empty string from a list of string using a for loop

Remove empty strings from a list of strings using the enumerate() function

In Python, the enumerate() function takes a collection and returns it as an enumerate object. This function adds a counter as the key of the enumerated object.

This is Python’s full source code to remove empty strings from a list of strings using the enumerate() function.

input_list_with_strings = [' ', 'Lidia', ' ', 'Jonny', 'Alster', ' ', 'Mitchell']
 
result_list = [i for a,i in enumerate(input_list_with_strings) if i!=' ']
     
print (result_list)

Output:

['Lidia', 'Jonny', 'Alster', 'Mitchell']

Refer to the screenshot below to see the output of the program to remove empty strings from a list of strings in Python using the enumerate().

Remove empty strings from a list of strings in python using the enumerate()

Conclusion

I hope this Python article provided sufficient information on how to remove empty strings from a list of strings in Python.

Here, I have explained different methods with illustrative examples like using list comprehension, remove() function, filter() function, filter()and lambda function, split(), for loop, and enumerate() function in detail.

I hope you will use the methods according to your requirements.

You may also like to read: