Remove all Empty Strings from a List of Strings in Python

avatar
Borislav Hadzhiev

Last updated: Feb 20, 2023
6 min

banner

# Table of Contents

  1. Remove empty strings from a list of Strings in Python
  2. Remove all empty elements from a list in Python

# Remove empty strings from a list of Strings in Python

Use a list comprehension to remove the empty strings from a list of strings.

The list comprehension will return a new list that doesn't contain any empty strings.

main.py
my_list = ['bobby', '', 'hadz', '', 'com', ''] new_list = [item for item in my_list if item] print(new_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com']

remove empty strings from list of strings

The example uses a list comprehension to remove the empty strings from a list.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we check if the current item is truthy and return the result.

main.py
my_list = ['bobby', '', 'hadz', '', 'com', ''] new_list = [item for item in my_list if item] print(new_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com']

Empty strings are falsy values, so they get filtered out.

The list comprehension creates a new list, it doesn't mutate the original list.

# Remove empty strings from a list of Strings with reassignment

If you want to remove the empty strings from the original list, use list slicing.

main.py
my_list = ['bobby', '', 'hadz', '', 'com', ''] my_list[:] = [item for item in my_list if item] print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com']

remove empty strings from list of strings with reassignment

We used the my_list[:] syntax to get a slice that represents the entire list, so we can assign to the variable directly.

The slice my_list[:] represents the entire list, so when we use it on the left-hand side, we are assigning to the entire list.

This approach changes the contents of the original list.

Alternatively, you can use the filter() function.

# Remove empty strings from a list of Strings using filter()

This is a three-step process:

  1. Use the filter() function to filter out the empty strings.
  2. Use the list() class to convert the filter object to a list.
  3. The new list won't contain any empty strings.
main.py
my_list = ['bobby', '', 'hadz', '', 'com', ''] new_list = list(filter(None, my_list)) print(new_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com']

remove empty strings from list of strings using filter

We used the filter() function to remove the empty strings from a list.

The filter function takes a function and an iterable as arguments and constructs an iterator from the elements of the iterable for which the function returns a truthy value.

If you pass None for the function argument, all falsy elements of the iterable are removed.

An empty string is a falsy value, so all empty strings get removed.

The last step is to use the list() class to convert the filter object to a list.

Alternatively, you can use a for loop.

# Remove empty strings from a list of Strings using for loop

This is a three-step process:

  1. Use a for loop to iterate over a copy of the list.
  2. On each iteration, check if the current item is an empty string.
  3. Use the list.remove() method to remove the empty strings.
main.py
my_list = ['bobby', '', 'hadz', '', 'com', ''] for item in my_list.copy(): if item == '': my_list.remove(item) print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com']

We used the list.copy() method to get a copy of the list.

main.py
my_list = ['bobby', '', 'hadz', '', 'com', ''] # ๐Ÿ‘‡๏ธ ['bobby', '', 'hadz', '', 'com', ''] print(my_list.copy())

The list.copy method returns a shallow copy of the object on which the method was called.

This is necessary because we aren't allowed to remove items from a list while iterating over it.

However, we can iterate over a copy of the list and remove items from the original list.

main.py
my_list = ['bobby', '', 'hadz', '', 'com', ''] for item in my_list.copy(): if item == '': my_list.remove(item) print(my_list) # ๐Ÿ‘‰๏ธ ['bobby', 'hadz', 'com']
On each iteration, we check if the current item is an empty string and use the list.remove() method to remove the matching elements.

The list.remove() method removes the first item from the list whose value is equal to the passed in argument.

The remove() method mutates the original list and returns None.

The most important thing to note when removing items from a list in a for loop is to use the list.copy() method to iterate over a copy of the list.

If you try to iterate over the original list and remove items from it, you might run into difficult to locate bugs.

# Remove all empty elements from a list in Python

You can also use a list comprehension to remove all empty elements from a list.

main.py
# โœ… Remove empty elements from a list (using list comprehension) my_list = ['a', '', [], (), None, 'b', '', 'c'] new_list = [item for item in my_list if item] print(new_list) # ๐Ÿ‘‰๏ธ ['a', 'b', 'c']

The example uses a list comprehension to remove the falsy values from a list.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.
main.py
my_list = ['a', '', [], (), None, 'b', '', 'c'] new_list = [item for item in my_list if item] print(new_list) # ๐Ÿ‘‰๏ธ ['a', 'b', 'c']

On each iteration, we check if the current item is truthy and return the result.

All values that are not truthy are considered falsy. The falsy values in Python are:

  • constants defined to be falsy: None and False.
  • 0 (zero) of any numeric type
  • empty sequences and collections: "" (empty string), () (empty tuple), [] (empty list), {} (empty dictionary), set() (empty set), range(0) (empty range).
In other words, the list comprehension filters out None, False, empty string, empty list, empty tuple, etc from the list.

The list comprehension creates a new list, it doesn't mutate the original list.

# Remove all empty elements from a list with reassignment

If you want to remove the empty elements from the original list, use list slicing.

main.py
my_list = ['a', '', [], (), None, 'b', '', 'c'] my_list[:] = [item for item in my_list if item] print(my_list) # ๐Ÿ‘‰๏ธ ['a', 'b', 'c']

We used the my_list[:] syntax to get a slice that represents the entire list, so we can assign to the variable directly.

The slice my_list[:] represents the entire list, so when we use it on the left-hand side, we are assigning to the entire list.

This approach changes the contents of the original list.

# Remove all empty elements from a list using filter()

Alternatively, you can use the filter() function.

main.py
my_list = ['a', '', [], (), None, 'b', '', 'c'] new_list = list(filter(None, my_list)) print(new_list) # ๐Ÿ‘‰๏ธ ['a', 'b', 'c']

We used the filter() function to remove the empty elements from a list.

The filter function takes a function and an iterable as arguments and constructs an iterator from the elements of the iterable for which the function returns a truthy value.

If you pass None for the function argument, all falsy elements of the iterable are removed.

The last step is to use the list() class to convert the filter object to a list.

# Remove all empty elements from a list using a for loop

Alternatively, you can use a for loop.

main.py
my_list = ['a', '', [], (), None, 'b', '', 'c'] for item in my_list.copy(): if not item: my_list.remove(item) print(my_list) # ๐Ÿ‘‰๏ธ ['a', 'b', 'c']
On each iteration, we check if the current item falsy and use the list.remove() method to remove the matching elements.

The list.remove() method removes the first item from the list whose value is equal to the passed in argument.

The remove() method mutates the original list and returns None.

The most important thing to note when removing items from a list in a for loop is to use the list.copy() method to iterate over a copy of the list.

If you try to iterate over the original list and remove items from it, you might run into difficult to locate bugs.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev