Last modified: May 21, 2026
Remove Empty Strings From Python List
Working with lists in Python is common. Sometimes your list contains empty strings. These empty strings can cause errors in your code. You need to clean them out.
This guide shows you how to remove empty strings from a list or array in Python. We will use simple methods. Each method is easy to understand for beginners.
Empty strings look like "" or ''. They are not the same as None or spaces. We focus on removing only empty strings here.
Method 1: Using List Comprehension
List comprehension is a clean and fast way to filter lists. It creates a new list. It keeps only items that are not empty.
# Original list with empty strings
my_list = ["apple", "", "banana", "", "cherry", ""]
# Remove empty strings using list comprehension
clean_list = [item for item in my_list if item != ""]
print(clean_list)
['apple', 'banana', 'cherry']
This method is short and readable. It works for any list. You can also use it for arrays from the array module if they contain strings.
Tip: List comprehension is often faster than loops. It is a Pythonic way to filter data.
Method 2: Using the filter() Function
The filter() function is built into Python. It applies a condition to each item. It returns only items that pass the test.
# Original list
my_list = ["cat", "", "dog", "", "fish"]
# Use filter to remove empty strings
clean_list = list(filter(None, my_list))
print(clean_list)
['cat', 'dog', 'fish']
When you pass None as the function, filter() removes all falsy values. Empty strings are falsy. This also removes 0, False, and None if they exist.
If you only want to remove empty strings, use a lambda function.
# Only remove empty strings, keep other falsy values
my_list = ["cat", "", 0, "dog", None, "fish"]
clean_list = list(filter(lambda x: x != "", my_list))
print(clean_list)
['cat', 0, 'dog', None, 'fish']
The filter() function is useful when you work with large data. It returns an iterator, which saves memory.
Method 3: Using a Loop
A simple for loop gives you full control. You can check each item and build a new list.
# Original list
my_list = ["sun", "", "moon", "", "star"]
# Create an empty list for clean data
clean_list = []
# Loop through each item
for item in my_list:
if item != "":
clean_list.append(item)
print(clean_list)
['sun', 'moon', 'star']
This method is clear for beginners. It shows the logic step by step. You can add extra conditions inside the loop easily.
Note: Loops are slower than list comprehension for large lists. But they are easier to read for new programmers.
Method 4: Using the remove() Method
The remove() method deletes the first occurrence of a value. You can use it in a loop to remove all empty strings.
# Original list
my_list = ["red", "", "green", "", "blue"]
# Keep removing empty strings until none left
while "" in my_list:
my_list.remove("")
print(my_list)
['red', 'green', 'blue']
This method modifies the original list. It is not the fastest for large lists. Use it only for small data.
Caution: Removing items from a list while iterating can cause bugs. Use a while loop or create a copy to avoid issues.
Method 5: Using join() and split()
If your list contains only strings, you can use join() and split(). This method is not common, but it works.
# Original list
my_list = ["a", "", "b", "", "c"]
# Join all items with a separator, then split again
clean_list = " ".join(my_list).split()
print(clean_list)
['a', 'b', 'c']
This method removes all empty strings. It also removes extra spaces. But it fails if your strings contain spaces.
Use this only for simple cases. It is not reliable for complex data.
Removing Empty Strings from Arrays
Python arrays from the array module hold numbers. But if you use a list of strings, the same methods work. For arrays from libraries like NumPy, use Boolean indexing.
import numpy as np
# NumPy array with empty strings
arr = np.array(["x", "", "y", "", "z"])
# Remove empty strings using Boolean indexing
clean_arr = arr[arr != ""]
print(clean_arr)
['x' 'y' 'z']
NumPy arrays are fast. Boolean indexing is the best way to filter them.
Which Method Should You Use?
Choose a method based on your needs:
- Use list comprehension for simple and fast filtering.
- Use filter() for memory efficiency with large data.
- Use a loop for learning or adding extra logic.
- Use remove() only for small lists.
- Use join() and split() only for simple string lists.
All methods work for lists and arrays. Pick the one that fits your code style.
Common Mistakes to Avoid
Beginners often confuse empty strings with whitespace. A string with a space " " is not empty. It contains a space character.
To remove whitespace-only strings, use strip():
my_list = ["hello", " ", "world", ""]
# Remove empty and whitespace-only strings
clean_list = [item for item in my_list if item.strip() != ""]
print(clean_list)
['hello', 'world']
Another mistake is modifying the list while iterating. Always create a new list or use a copy.
For more string manipulation tips, check our guide on Python String Replace: Change Characters Easily.
Conclusion
Removing empty strings from a list or array in Python is simple. You have many options. List comprehension is the most popular and readable. The filter() function is great for large data. Loops help you understand the process.
Always test your method with sample data. Make sure it removes only what you want. Avoid common mistakes like confusing empty strings with spaces.
Now you can clean your lists easily. Practice with different methods. Find the one that works best for your project.
If you work with strings often, read our beginner guide on Python String Functions Guide for Beginners for more helpful techniques.