Python program to remove null value from a list

This article discusses how to remove null values from a Python list. Before proceeding, it is necessary to understand what a null value is? What a List is? Why it is necessary to remove null value from list. Let’s move ahead with the topic and understand them in detail. Let’s start with the Null value.

Null Value is defined as a parameter that does not have a value. It indicates the absence of a value. Python has a special value for "Null" that shows a variable doesn't point to anything.

But in Python there is no null, there is NONE representing the absence of value.

None In Python

NONE is a keyword in which represents an empty string or missing value having no output. In Python None is an object of the NONE TYPE CLASS and first-class citizen in python which is not defined to be 0 or any other value.

  • None can be assigned to any variable.

  • It is not considered as an empty string in python.

  • It is not same as 0, False, or an empty string.

  • Only None can be None.

Representation of None

None can be written simply without performing any operation as None by declaring a variable equals to None.

variable = None

What is list in python?

There are 4 built-in datatypes in python (List, Tuple, Dictionary, set). List is one of them which is used to store multiple items in a single variable. They are created using square brackets [ ]. Lists are Mutable which means they are changeable; they have the ability to change their values. It is useful for insertion and deletion operations.

Why it is necessary to remove none in python?

Suppose we have a large dataset, and on analyzing it we find missing values or no values. To ensure that data is clean, such null values must be removed from dataset. Null values affect the performance and accuracy of the model, so it is necessary to remove unwanted null values from the data before processing to make it effective.

How to remove none from python list?

We can remove none in Python by the following five methods. Let’s discuss them one by one with the help of examples.

Using Filter() Method

In this method, we filter the none values which include empty lists. It is the easiest way to remove null values from list in python.


The following example uses filter() method to remove all of the empty strings from my_list and re-assign them back to my_list.my_list = ['hi','Joy','','whats','','up'] as shown below.

my_list = ['hi', 'Joy', '', 'whats', '', 'up']
my_list = list(filter(None,my_list))


The following output is generated when we execute the above -

['hi', 'Joy', '', 'whats', '', 'up']
['hi', 'Joy', 'whats', 'up']

Using Naïve Method

The naïve method can also be used to remove none from list. We use for loop and check whether the values are none, if it is so then ignored and the rest values can be appended to another list.


In the following program, a list is initialized with several values including two None values. A new empty list is been created and a for loop runs through the original list and checks each element to see if it is equal to None, using the naïve method.

# List with none values
my_list = [4,5, True, False, None, 'List', None]

# Initialize filtered list
new_list = []

# Using for loop
for ele in my_list:
   if ele != None:
print ("List with None values: ", my_list)
print ("List without None values: ", new_list)


Upon executing the above program, the output is generated as follows –

List with None values: [4, 5, True, False, None, 'List', None]
List without None values: [4, 5, True, False, 'List']

Using join() method

We have another method to remove None from list is join( ) function.


Following is an example to use join() method to remove None from the list

myList = ['', 'Salad','is', '','Good', '']
print("Contents of the List : ",myList)
# Remove Empty Value from List
newList = ' '.join(myList).split()
print("Contents of the List after removing  nulls: ",newList)


Execute the above program to see the output as follows -

Contents of the List :  ['', 'Salad', 'is', '', 'Good', '']
Contents of the List after removing  nulls:  ['Salad', 'is', 'Good']

Using List comprehension

We use list comprehension to remove null values. It is a way to create a new list from existing datatypes like lists, tuples, and sets. In this method we iterate through the list, but we don’t include the list which is empty.


Following is an example to show how to remove null values using list comprehension.

# List with none values
my_list = [0,1, True, None, False]

# Using List comprehension to remove none values
new_list = [ ele for ele in my_list if ele is not None ]
print("List with None values: ", my_list)
print("List without None values", new_list)


Execute the above program to see the output as follows -

List with None values: [0, 1, True, None, False]
List without None values [0, 1, True, False]

Using Enumerate function

Enumerate is an in-built function that is used to loop over a list, tuple, dictionary and string. It allows to iterate through the sequence but keeps track on both index and the element.


In the following program, a list called my_list is created which contains strings. Then, a new list is created from the existing one, with all blank spaces removed and modifies the list.

my_list = [' ', 'myfile', ' ', 'is', 'running', ' ']
my_list = [i for a,i in enumerate(my_list) if i!=' ']
print (my_list)


Executing the above program generates the following output.

['myfile', 'is', 'running']


In this article, we discuss various methods to remove null from the list. I simply explained Step by step the removal of null values. We have seen all the different Methods which can help us to remove null values from the list like filter(), list comprehension, naïve method, join method and enumerate function.

Updated on: 24-Apr-2023

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started