Remove None values from a list in Python

avatar

Borislav Hadzhiev

Last updated: Aug 19, 2022

banner

Photo from Unsplash

Remove None values from a list in Python #

Use a list comprehension to remove the None values from a list in Python, e.g. new_list = [i for i in my_list if i is not None]. The new list will contain all values from the original list, except for the None values.

main.py
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.

We used a list comprehension to select all of the items from the original list that are not 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.

Remove None values from a list using filter() #

To remove None values from a list:

  1. Use the filter() function to filter out the None values.
  2. Use the list() class to convert the filter object to a list.
  3. The new list won't contain any None values.
main.py
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.

The lambda function gets called with each item from the list, checks if the value is not 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.

Remove None values from a list using a for loop #

To remove None values from a list:

  1. Declare a new variable that stores an empty list.
  2. Iterate over the original list.
  3. Check if each item is not None.
  4. Use the list.append() method to append items that meet the condition to the new list.
main.py
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.

To remove None values from a list:

  1. Use a for loop to iterate over a copy of the list.
  2. Check if each value is None.
  3. Use the list.remove() method to remove the None values from the list.
main.py
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.

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

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

main.py
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.

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.