How to Filter out none from List in Python : Different Methods

A list is a data structure where you can store multiple variables in a single variable. The list may also contain None as the element. In this entire tutorial you will learn Filter out none from List in Python using various methods.

Methods to Filter out none from List in Python

Let’s know all the methods that will useful for you to remove no element from the list.

Method 1: Using the filter() function

Using the filter() function in Python, you can remove elements from a list that fulfill a specific condition. To remove None values from a list using the following lines of code.

my_list = [10, 20, None, 30, None, 40]
filtered_list = list(filter(None, my_list))
print(filtered_list)
removing none from list using filter() function
removing none from list using filter() function

Method 2: Filter out none from List in Python using list comprehension

Creating a new list without any None elements can be done by using list comprehension to filter out the None values from the original list.

my_list =[10, 20, None, 30, None, 40]
filtered_list = [i for i in my_list if i is not None]
print(filtered_list)

Output

[10,20,30,40]

Method 3: Use filter() with lambda function

One can also use the filter() and lambda functions to remove the none value from the list.

my_list = [10, 20, None, 30, None, 40]
filtered_list = list(filter(lambda x: x is not None, my_list))
print(filtered_list)

Output

[10,20,30,40]

Method 4 : Use filterfalse() with lambda function

The itertools module provides the filterfalse() function that can be used to remove None values from a list. This works in the same way as the filter() but instead, it returns elements from the input iterable for which the function produces a False result.

my_list = [10, 20, None, 30, None, 40]
from itertools import filterfalse
filtered_lst = list(filterfalse(lambda x: x is None, my_list))
print(filtered_lst)

Output

[10,20,30,40]

Method 5: Use generator expression

If your list is very large, you can also filter out None values using a generator expression. It allows you to save memory.

my_list = [10, 20, None, 30, None, 40]
filtered_lst = (x for x in my_list if x is not None)
print(list(filtered_lst))

Output

[10,20,30,40]

Conclusion

There are multiple method for eliminating None values from a list in Python. The most general way is to use a list comprehension, the filter() function with a lambda function, or the filterfalse() function from the itertools module. Alternately, a generator expression can also be used to filter out None values, which is ideal if the list is quite large. Every method offers a convenient and effective way of removing None values from a list and can be easily adapted to match the particular needs of the program.

Hi, I am CodeTheBest. Here you will learn the best coding tutorials on the latest technologies like a flutter, react js, python, Julia, and many more in a single place.

SPECIAL OFFER!

This Offer is Limited! Grab your Discount!
15000 ChatGPT Prompts
Offer Expires In:
close-link