Skip to main content

How to Remove Empty Strings from a List in Python

How to Remove Empty Strings from a List in Python | Rustcode

How to Remove Empty Strings from a List in Python

When working with lists of strings in Python, you may encounter unwanted empty strings, which can interfere with sorting, filtering, or data analysis. This article shows several efficient ways to remove empty strings from a list, with code samples and clear explanations.


Why Remove Empty Strings from Lists?

  • Cleaner Data: Prevent processing or displaying blank values.
  • Accurate Analysis: Get correct counts, summaries, or statistics.
  • Better UX: Improve user interface by hiding empty results or records.

01. Using List Comprehension (Recommended)

The most Pythonic and efficient way is a list comprehension that filters out empty strings.

mylist = ["", "Welcome", "", "to", "", "Rustcode", ""]
filtered = [s for s in mylist if s]
print(filtered)

Output:

['Welcome', 'to', 'Rustcode']
Explanation:
  • if s skips all elements that are logically False (empty strings "" evaluate to False).
  • Fast for large lists; keeps all non-empty entries in the original order.

02. Using filter() Function

The built-in filter() removes empty strings by keeping only values that are True. Wrap it with list() for a new list.

mylist = ["", "Welcome", "", "to", "", "Rustcode", ""]
result = list(filter(None, mylist))
print(result)

Output:

['Welcome', 'to', 'Rustcode']
Explanation:
  • filter(None, ...) removes all "falsy" items (here, empty strings).
  • Clean, functional, and ideal for one-liners or pipelines.

03. Using remove() in a Loop (In-Place)

If you want to remove empty strings from the original list in place (without creating a new list), use remove() inside a while loop:

mylist = ["", "Welcome", "", "to", "", "Rustcode", ""]
while "" in mylist:
    mylist.remove("")
print(mylist)

Output:

['Welcome', 'to', 'Rustcode']
Explanation:
  • remove("") deletes only the first occurrence per call, so you loop until all are gone.
  • This changes the original list, not a copy.

04. Removing Strings with Only Whitespace

Sometimes, you also want to drop strings that contain only spaces or tabs (not just empty ""). Use strip() with a comprehension:

mylist = ["", "Welcome", "  ", "to", "\t", "Rustcode", ""]
# Only keep non-empty, non-whitespace strings
result = [s for s in mylist if s.strip()]
print(result)

Output:

['Welcome', 'to', 'Rustcode']
Explanation:
  • s.strip() removes spaces/tabs; empty results are excluded.
  • Perfect for cleaning up inputs or data files where lines may have spaces but no content.

05. Comparison Table: Methods to Remove Empty Strings

Method Removes Empty Strings Removes Whitespace-only Strings Modifies Original List Best For
List comprehension
[s for s in mylist if s]
Yes No No Simple, fast cleanup
filter(None, ...) Yes No No Pipelined/functional code
while ... remove("") Yes No Yes Changing original list
List comp with strip
[s for s in mylist if s.strip()]
Yes Yes No Data cleaning

Conclusion

Python gives you several handy methods to remove empty strings from lists—list comprehensions, filter(), and remove(). Choose the approach that fits your project best: comprehension for clarity and speed, filter() for functional chains, or remove() when you want to update lists in place. Use strip() if you need to eliminate strings that are only whitespace for cleaner results.

Comments