Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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

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

Use filter() for a predicate-based alternative

For a string-only list, filter() can apply the same exact-empty-string test:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
result = 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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.