Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk2 min

Python Lists: Remove Empty Strings Without Losing Whitespace or Other Values

Use a list comprehension to remove exact empty strings from a Python list. Learn when to filter whitespace too, use filter(), or update the original list in place.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To remove only exact empty strings from a Python list, use a list comprehension with an explicit comparison:

items = ["apple", "", "banana", ""]
result = [item for item in items if item != ""]
# ['apple', 'banana']

This creates a new list, keeps the original order, and leaves whitespace-only strings, None, and other values untouched.

As an Amazon Associate I earn from qualifying purchases.

Remove only zero-length strings

The condition item != "" excludes strings whose length is zero. Every other value passes the test, including " " (a space), None, 0, and False. That makes this the clearest option when “empty” means exactly "".

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

A list comprehension builds a new list from the items that meet its condition. See the Python built-in types documentation.

Remove whitespace-only strings too

If entries containing only whitespace should also be discarded, test each string with strip():

items = ["apple", "", "   ", " banana "]
result = [item for item in items if item.strip()]
# ['apple', ' banana ']

strip() returns a copy with leading and trailing whitespace removed. Here, the trimmed copy is used only for the condition: retained strings stay unchanged, so " banana " keeps its surrounding spaces. If you want to normalize retained strings as well, put the transformation in the output expression:

result = [item.strip() for item in items if item.strip()]

This version removes whitespace-only entries and trims whitespace from every retained string.

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.

Choose a filter that matches the data

Goal Expression What it removes
Exact empty strings [item for item in items if item != ""] Only ""
Empty and whitespace-only strings [item for item in items if item.strip()] "" and strings that become empty after trimming
All falsy values list(filter(None, items)) Every value that evaluates as false, not just empty strings

filter(None, items) can also discard values such as None, 0, and False. Use it only when removing every falsy value is actually the goal. Python’s Functional Programming HOWTO explains predicate-based filtering and its list-comprehension equivalent.

Use filter() for an explicit predicate

For a string-only list, this produces the same result as the exact-empty-string comprehension:

result = list(filter(lambda item: item != "", items))

filter() returns an iterator, so list() materializes it as a list. For a simple condition, the comprehension usually makes the rule easier to see at a glance.

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

Replace the original list when needed

A comprehension assigns a new list to result; it does not change items. If other parts of your program hold a reference to items and must see the filtered contents, assign to the full slice instead:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
items[:] = [item for item in items if item != ""]

This replaces the contents while preserving the identity of the list object. 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. Python’s data structures tutorial documents list operations.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.