How to remove None from list in Python

Remove None from list in Python

Hello Techies,

In this article, we are going to check How to remove None from list with the help of examples.

What is none in python?

  • None keyword is an object and is a data type of none type class.
  • None datatype doesn’t contain any value.
  • None keyword is used to define a null variable or object.
  • None keyword is immutable.

Check this link for more information on None datatype.


How to remove None from list

Method 1: Remove None from list using for loop

The for loop is the most common loop in the Python programming language. The For Loop is useful to iterate over the elements of a sequence such as string, list, tuple, etc.

let’s see how can we remove None from list using for loop.

Example: How to remove None from list using for loop?

x = [True, "Python", None, False, 1, True, 0, None]
result = []
for each in x:
    if each != None:
        result.append(each)
print(result)

Output:

[True, 'Python', False, 1, True, 0]

Method 2: Remove None from list using list comprehension

We also use list comprehensions to remove any values ​​from the list. In Python, list comprehensions are a way to create new lists from existing other iterations such as lists, sets, and tuples. We can also say that list comprehension is a short version of for loop. You can visit this link to know about list comprehension.

Example: How to remove None from list using list comprehension?

x = [True, "Python", None, False, 1, True, 0, None]
result = [each for each in x if each is not None]
print(result)

Output:

[True, 'Python', False, 1, True, 0]

Method 3: Remove None from list using filter()

The easiest way to remove anything from a list in Python is to use the list filter() method.

The Python filter() function returns an iterator where the items are filtered by the function to check if the item is accepted or not. This method takes 2 parameters as function and iterator.

Example: How to remove None from list using filter()?

x = [True, "Python", None, False, 1, True, 0, None]
result = list(filter(lambda item: item is not None, x))
print(result)

Output:

[True, 'Python', False, 1, True, 0]

Method 4: Remove None from list using filter() + __ne__

Example: How to remove None from list using filter() + __ne__

x = [True, "Python", None, False, 1, True, 0, None]
result = filter(None.__ne__, x)
print(list(result))

Output:

[True, 'Python', False, 1, True, 0]

Method 4: Remove None from list using filter() + is_not + partial function

Now will check other interesting way to remove None from list.

Example: How to remove None from list using filter() + is_not + partial function

from operator import is_not
from functools import partial

x = [True, "Python", None, False, 1, True, 0, None]
result = filter(partial(is_not, None), x)
print(list(result))

Output:

[True, 'Python', False, 1, True, 0]

I hope you understand all the methods used to remove None from list. Comment below if you have any doubt.

Leave a Comment