Last updated: Apr 8, 2024
Reading timeยท6 min
Use a list comprehension to remove the None values from a list in Python.
The new list will contain all values from the original list, except for the
None
values.
my_list = [1, None, 3, None, 8, None] new_list = [i for i in my_list if i is not None] print(new_list) # ๐๏ธ [1, 3, 8]
List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.
None
.This solution doesn't mutate the original list, it returns a new list that
doesn't contain any None
values.
Alternatively, you can use the filter
function.
This is a three-step process:
filter()
function to filter out the None
values.list()
class to convert the filter object to a list.None
values.my_list = [1, None, 3, None, 8, None] new_list = list(filter(lambda x: x is not None, my_list)) print(new_list) # ๐๏ธ [1, 3, 8]
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
and returns the result.The last step is to use the list() class to
convert the filter
object to a list.
Alternatively, you can use a simple for loop.
This is a four-step process:
None
.list.append()
method to append items that meet the condition to the
new list.my_list = [1, None, 3, None, 8, None] new_list = [] for item in my_list: if item is not None: new_list.append(item) print(new_list) # ๐๏ธ [1, 3, 8]
We used a for
loop to iterate over the list.
On each iteration, we check if the current item is not None and if the condition is met, we append the item to the new list.
The new list won't contain any None
values.
Alternatively, you can mutate the original list.
while
loopYou can also use a while
loop to remove the None
values from a list.
my_list = [1, None, 3, None, 8, None, None, None, None] while None in my_list: my_list.remove(None) print(my_list) # ๐๏ธ [1, 3, 8]
The condition of the while
loop iterates for as long as there are None
values in the list.
On each iteration, we use the list.remove()
method to remove a None
value
from the list.
Once the condition is no longer met and the list doesn't contain any None
values, the while
loop exits.
This is a three-step process:
for
loop to iterate over a copy of the list.None
.list.remove()
method to remove the None
values from the list.my_list = [1, None, 3, None, 8, None, None, None] for item in my_list.copy(): if item is None: my_list.remove(item) print(my_list) # ๐๏ธ [1, 3, 8]
This example removes the None
values from the original list.
We used the list.copy()
method to get a copy of the list.
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 elements from the original list.
my_list = [1, None, 3, None, 8, None, None, None] for item in my_list.copy(): if item is None: my_list.remove(item) print(my_list) # ๐๏ธ [1, 3, 8]
On each iteration, we check if the current item is None
and if the condition
is met, we use the list.remove()
method to remove it.
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 while iterating
is to use the list.copy()
method to iterate over a copy of the list.
If you try to remove elements from the original list while iterating over it, you might run into difficult-to-locate bugs.
You can also use the filterfalse
class from the itertools
module to remove
the None
values from a list.
from itertools import filterfalse my_list = [1, None, 3, None, 8, None, None] def null_check(value): return value is None my_list[:] = filterfalse(null_check, my_list) print(my_list) # ๐๏ธ [1, 3, 8]
Make sure to import the filterfalse
class from the itertools
module as shown
in the code sample.
The filterfalse
method takes a predicate and an iterable and calls the
function with each item in the iterable.
The method returns a list containing the items for which the function returned
false
.
In the example, the method returns a list containing the items that are not
None
.
You can use a list comprehension if you need to replace the None values in a list.
my_list = ['a', None, 'b', None, 'c', None, None] # ๐๏ธ replace None values in a list with empty string new_list_1 = ['' if i is None else i for i in my_list] print(new_list_1) # ๐๏ธ ['a', '', 'b', '', 'c', '', ''] # ๐๏ธ replace None values in a list with 0 new_list_1 = [0 if i is None else i for i in my_list] print(new_list_1) # ๐๏ธ ['a', 0, 'b', 0, 'c', 0, 0]
The first example replaces None
values in a list with an empty string and the
second replaces None
with 0
.
We basically iterate over the list and return an empty string if the item of the
current iteration is None
, otherwise we return the item as is.
You can use this approach to replace None
values with any other value.
for
loopAlternatively, you can use a for
loop to replace None values in a list.
my_list = ['a', None, 'b', None, 'c', None, None] for index, item in enumerate(my_list): if item is None: my_list[index] = '' # ๐๏ธ replaces None with empty string print(my_list) # ๐๏ธ ['a', '', 'b', '', 'c', '', '']
The enumerate function takes an iterable and returns an enumerate object containing tuples where the first element is the index and the second is the item.
my_list = ['apple', 'banana', 'melon'] for index, item in enumerate(my_list): print(index, item) # ๐๏ธ 0 apple, 1 banana, 2 melon
On each iteration, we check if the item is None
, and if it is, we update the
list item at the specific index.
I've also written an article on how to remove the None values from a dictionary.
You can learn more about the related topics by checking out the following tutorials: