Last updated: Apr 9, 2024
Reading timeยท6 min
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.
my_list = ['bobby', '', 'hadz', '', 'com', ''] new_list = [item for item in my_list if item] print(new_list) # ๐๏ธ ['bobby', 'hadz', 'com']
The example uses a list comprehension to remove the empty strings from a list.
On each iteration, we check if the current item is truthy and return the result.
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.
If you want to remove the empty strings from the original list, use list slicing.
my_list = ['bobby', '', 'hadz', '', 'com', ''] my_list[:] = [item for item in my_list if item] print(my_list) # ๐๏ธ ['bobby', 'hadz', 'com']
We used the my_list[:]
syntax to get a slice that represents the entire list,
so we can assign to the variable directly.
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.
This is a three-step process:
filter()
function to filter out the empty strings.list()
class to convert the filter
object to a list.my_list = ['bobby', '', 'hadz', '', 'com', ''] new_list = list(filter(None, my_list)) print(new_list) # ๐๏ธ ['bobby', 'hadz', 'com']
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.
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.
This is a three-step process:
for
loop to iterate over a copy of the list.list.remove()
method to remove the empty strings.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.
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.
However, we can iterate over a copy of the list and remove items from the original list.
my_list = ['bobby', '', 'hadz', '', 'com', ''] for item in my_list.copy(): if item == '': my_list.remove(item) print(my_list) # ๐๏ธ ['bobby', 'hadz', 'com']
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.
You can also use a list comprehension to remove all empty elements from a list.
# โ 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.
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:
None
and False
.0
(zero) of any numeric type""
(empty string), ()
(empty tuple), []
(empty list), {}
(empty dictionary), set()
(empty set), range(0)
(empty
range).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.
If you want to remove the empty elements from the original list, use list slicing.
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.
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.
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.
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.
Alternatively, you can use a for
loop.
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']
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.
You can learn more about the related topics by checking out the following tutorials: