How to Remove the None values from a List in Python

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
6 min

banner

# Table of Contents

  1. Remove None values from a list in Python
  2. Remove None values from a list using filter()
  3. Remove None values from a list using a for loop
  4. Remove None values from the original list using a while loop
  5. Remove None values from the original list, in place
  6. Remove None values from the original list using filterfalse()
  7. Replace None values in a List in Python

# Remove None values from a list in Python

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.

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]

remove none values from list

The code for this article is available on GitHub

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()

This is a three-step process:

  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]

remove none values from list using filter

The code for this article is available on GitHub

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

This is a four-step process:

  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]

remove none values from list using for loop

The code for this article is available on GitHub

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.

# Remove None values from the original list using a while loop

You can also use a while loop to remove the None values from a list.

main.py
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 code for this article is available on GitHub

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.

# Remove None values from the original list, in place

This is a three-step process:

  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]
The code for this article is available on GitHub

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.

# Remove None values from the original list using filterfalse()

You can also use the filterfalse class from the itertools module to remove the None values from a list.

main.py
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]
The code for this article is available on GitHub

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.

# Replace None values in a List in Python

You can use a list comprehension if you need to replace the None values in a list.

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

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

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.

# Replace None values in a List using a for loop

Alternatively, you can use a for loop to replace None values in a list.

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

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

# 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