Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to remove empty strings from a list of strings in Python?
The Lists in Python are used to store collections of items such as numbers, strings. While working with lists of strings, it is common to find empty strings in the list.
An empty string is a string value with zero length (""). While they are present as elements in the list, they do not hold any meaningful data and can interfere with operations like sorting and filtering. Removing empty strings ensures the list contains only valid values.
Python provides several methods to remove empty strings from a list. Let's explore the most effective approaches ?
Using filter() Function
The filter() function filters elements from an iterable based on a specified condition. When passed None as the function parameter, it removes all falsy values including empty strings.
Syntax
filter(function, iterable)
Example
Here's how to remove empty strings using filter() ?
str_list = ["Tutorialspoint", "", "Welcomes", "", "Everyone", ""]
print("Original list:", str_list)
updated_list = list(filter(None, str_list))
print("After removing empty strings:", updated_list)
Original list: ['Tutorialspoint', '', 'Welcomes', '', 'Everyone', ''] After removing empty strings: ['Tutorialspoint', 'Welcomes', 'Everyone']
Using remove() Method with while Loop
The remove() method searches for a specific element and removes the first matching occurrence. We use a while loop to repeatedly remove all empty strings.
Syntax
list.remove(obj)
Example
This approach removes empty strings one by one until none remain ?
str_list = ["Ciaz", "", "Cruze", "", "Cheron", ""]
print("Original list:", str_list)
while ("" in str_list):
str_list.remove("")
print("After removing empty strings:", str_list)
Original list: ['Ciaz', '', 'Cruze', '', 'Cheron', ''] After removing empty strings: ['Ciaz', 'Cruze', 'Cheron']
Using join() and split() Methods
The join() method combines list elements into a single string, while split() method breaks a string into a list. This combination automatically removes empty strings.
Example
Join the list with spaces, then split back into a list. Empty strings disappear in the process ?
str_list = ["HTML", "", "Python", "", "Java", ""]
print("Original list:", str_list)
updated_list = ' '.join(str_list).split()
print("After removing empty strings:", updated_list)
Original list: ['HTML', '', 'Python', '', 'Java', ''] After removing empty strings: ['HTML', 'Python', 'Java']
Using List Comprehension
List comprehension provides a concise way to filter out empty strings by creating a new list with only non-empty elements ?
str_list = ["Apple", "", "Banana", "", "Cherry", ""]
print("Original list:", str_list)
updated_list = [item for item in str_list if item]
print("After removing empty strings:", updated_list)
Original list: ['Apple', '', 'Banana', '', 'Cherry', ''] After removing empty strings: ['Apple', 'Banana', 'Cherry']
Comparison
| Method | Performance | Readability | Modifies Original |
|---|---|---|---|
filter() |
Fast | Good | No |
remove() |
Slow for large lists | Good | Yes |
join()/split() |
Medium | Fair | No |
| List comprehension | Fast | Excellent | No |
Conclusion
Use filter(None, list) for simple and efficient removal of empty strings. List comprehension offers the best balance of readability and performance. Avoid remove() for large lists due to performance concerns.
