iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
Use a list comprehension with an explicit comparison to remove only zero-length strings:
items = ["apple", "", "banana", ""]
result = [item for item in items if item != ""]
# ['apple', 'banana']
This creates a new list, keeps the order of retained items, and leaves whitespace-only strings, None, and other values untouched.
Choose what counts as empty
“Empty” can mean an exact empty string or a string that contains only whitespace. Pick the condition that matches your data; these options do not remove the same values.
| What to remove | Condition | Effect |
|---|---|---|
| Exact empty strings | item != "" |
Removes ""; keeps values such as " ". |
| Empty and whitespace-only strings | item.strip() |
Removes strings that become empty after leading and trailing whitespace is stripped; keeps the original text of retained strings. |
| All falsy values | filter(None, items) |
Removes every value that evaluates to false, not just empty strings. |
Remove exact empty strings
items = ["apple", "", " ", "banana"]
result = [item for item in items if item != ""]
# ['apple', ' ', 'banana']
The explicit comparison is the clearest choice when the list may contain non-string values or when whitespace must be preserved. Python documents list comprehensions as a way to construct lists. Python built-in types documentation
#1 Best Overall
Remove whitespace-only strings too
items = ["apple", " ", "banana", ""]
result = [item for item in items if item.strip()]
# ['apple', 'banana']
strip() returns a copy of a string with leading and trailing characters removed. Used as a condition, it filters out a string made only of whitespace without changing the retained string itself. This example assumes the list contains strings.
Filter all falsy values only when that is intended
items = ["apple", "", 0, False, None]
result = list(filter(None, items))
# ['apple']
This also removes 0, False, and None, so it is broader than removing empty strings. Python’s Functional Programming HOWTO explains predicate-based filtering and its comprehension equivalent. Python Functional Programming HOWTO
Rank #2
Use filter() for a predicate-based alternative
For a string-only list, filter() can apply the same exact-empty-string test:
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Scan for outdated or missing drivers - takes under a minute3Repair Windows errors before they cause bigger problemsresult = list(filter(lambda item: item != "", items))
filter() produces an iterator, so wrap it in list() when you need a list immediately. For this straightforward condition, the list comprehension is generally easier to read.
Update the existing list instead of creating a new one
A comprehension makes a separate list. If other parts of your program hold a reference to items and need to see the filtered contents, assign the result to the full slice:
items[:] = [item for item in items if item != ""]
Slice assignment replaces the contents of the existing list, preserving the list object’s identity. Python’s data-structures tutorial documents list operations and mutation. Python data structures tutorial
Avoid removing matching elements with remove() while looping over that same list: deletion shifts later elements, which can cause a loop to skip a match.
Free tools Windows power users keep installed
One-click scans. No signup required.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

