Skip to main content

How to Remove Empty Strings and Falsy Values from Python Lists

Cleaning data often involves removing empty strings (''), None values, or other "empty" or "falsy" elements from Python lists.

This guide explores effective and Pythonic methods for achieving this, including list comprehensions, the filter() function, and for loops, explaining the difference between creating new lists and modifying them in-place.

Understanding Falsy Values in Python

Before removing "empty" elements, it's crucial to understand what Python considers "falsy". A falsy value evaluates to False in a boolean context (like an if statement). The falsy values are:

  • None
  • False
  • Numerical zero (0, 0.0, 0j)
  • Empty sequences and collections ('', [], (), {}, set(), range(0))

All other values are considered "truthy".

Removing Empty Strings ('')

These methods focus specifically on removing only the empty string ''.

This is generally the most readable and Pythonic way to create a new list without empty strings:

my_list = ['tutorial', '', 'reference', '', 'com', '']
new_list = [item for item in my_list if item != '']
print(new_list) # Output: ['tutorial', 'reference', 'com']
  • The list comprehension iterates through the list and includes only those items which are not equal to the empty string.

Using filter() with a Lambda (Less Common for Just '')

While filter(None, ...) removes all falsy values, you can use a lambda for specific filtering:

my_list = ['tutorial', '', 'reference', '', 'com', '']
new_list = list(filter(lambda item: item != '', my_list))
print(new_list) # Output: ['tutorial', 'reference', 'com']

Using a for Loop (Modifying In-Place)

To modify the original list, iterate over a copy:

my_list = ['tutorial', '', 'reference', '', 'com', '']
for item in my_list.copy(): # Iterate over a copy
if item == '':
my_list.remove(item) # Remove from original
print(my_list) # Output: ['tutorial', 'reference', 'com']
warning

Remember: Never modify a list while iterating directly over it. Always iterate over a copy (my_list.copy() or my_list[:]) if removing items from the original list within the loop.

Removing All Falsy Elements (Including None, [], (), 0, etc.)

These methods remove all values that Python considers falsy.

Leverage the implicit boolean check for truthiness:

my_list = ['a', '', [], (), None, 'b', 0, 'c', False]
new_list = [item for item in my_list if item] # Keep only truthy items
print(new_list) # Output: ['a', 'b', 'c']
  • if item checks if the item is truthy. Falsy items (like '', [], None, 0, False) are excluded.

Using filter(None, ...)

This built-in function provides a concise way to filter out all falsy values:

my_list = ['a', '', [], (), None, 'b', 0, 'c', False]
new_list = list(filter(None, my_list))
print(new_list) # Output: ['a', 'b', 'c']
  • Passing None as the first argument to filter() tells it to use the identity function, effectively keeping only truthy elements.

Using a for Loop (Modifying In-Place)

Iterate over a copy and remove falsy items from the original:

my_list = ['a', '', [], (), None, 'b', 0, 'c', False]
for item in my_list.copy():
if not item: # Check if the item is falsy
my_list.remove(item)
print(my_list) # Output: ['a', 'b', 'c']

Important Note: Modifying vs. Creating New Lists

  • List comprehensions and filter() create new lists. The original list remains unchanged.
  • Using a for loop with .remove() on a copy modifies the original list in-place.
  • Assigning back to a full slice (my_list[:] = [item for item in my_list if item]) also modifies the original list in-place.

Choose the approach based on whether you need to preserve the original list or modify it directly. Creating new lists is often safer and preferred in functional programming paradigms.

Conclusion

This guide demonstrated several ways to remove empty strings and other falsy values from Python lists.

List comprehensions ([item for item in my_list if item]) and filter(None, my_list) are the most concise and Pythonic methods for creating new lists without falsy elements.

  • If you need to modify the list in-place, remember to iterate over a copy while removing items from the original list using a for loop.
  • Understanding Python's concept of truthy and falsy values is key to using these filtering techniques effectively.