How to Remove None from List in Python : Know the various Methods

How to Remove None from List in Python

A list is a data structure that allows you to create a mutable or changeable ordered sequence of elements. In python, it is created using the square bracket []. Inside it, each element is separated by a comma. The value inside the list is known as an element. But there can be also values that do not represent anything that is None. In this entire tutorial, you will learn the various ways to remove none from the list in python.

 

But before going to the methods section let’s create a sample list that contains None value in it.  Execute the below lines of code to create a sample array.

sample_list = [10,20,30,None,40,None,50]
print(sample_list)

Output

Sample list contain None as a item
The sample list contains None as an item

Methods to Remove None from List in Python

Let’s know the various methods to remove the None value from the list.

Method 1 : Use the fliter() function

Python provides the filter() function that accepts the one argument as a function and the second argument as the list for applying that function.

Run the below lines of code to return a new list that will contain only numbers without None value.

sample_list = [10,20,30,None,40,None,50]
final_list = filter(None, sample_list)
list(final_list)

Output

[10, 20, 30, 40, 50]

Method 2:  Use List Comprehension

Another method to remove None from List in Python is List comprehension. It is a powerful feature of python that allows you to create a new list from the existing list after doing some manipulation on it.

Execute the below lines of code to remove the None value.

sample_list = [10,20,30,None,40,None,50]
final_list = [i for i in sample_list if i is not None]
print(final_list)

Output

[10, 20, 30, 40, 50]

Method 3: Using the remove() function

The third method for removing the None from the List in python is the use of the remove() function. Here you have to iterate each element of the list and use the remove() method to remove the element containing the None value.

sample_list = [10,20,30,None,40,None,50]
for ele in sample_list:
  if ele is None:
    sample_list.remove(ele)

print(sample_list)

Output

[10, 20, 30, 40, 50]

Method 4: Using the count() function

The count() function allows you to count the number of elements in the list. As the None does not count to value therefore to remove the None value you will use this function.

Run the below lines of code.

sample_list = [10,20,30,None,40,None,50]
final_list = [i for i in sample_list if sample_list.count(i) != 0]
print(final_list)

Output

[10, 20, 30, 40, 50]

Method 5: Remove None from List in Python using map() function

The fifth method to remove None from the List is the use of map() function. Inside the map() function the first argument will be lambda function and the second is the input list.

It will returns the new list without the None value.

sample_list = [10,20,30,None,40,None,50]
final_list = map(lambda i: i if i is not None else '', sample_list)
print(list(final_list))

Output

[10, 20, 30, '', 40, '', 50]

 

Conclusion

The list is a widely used data structure in Python projects. Sometimes you may get the None values in the list and this makes it difficult to do some manipulation on it. So if you want to remove the None from the list then use the above methods.

I hope you have liked this tutorial. If you have any other methods then you can contact us for adding them here.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner