Python | Remove None values from list
Last Updated :
17 Apr, 2023
There can be multiple methods to remove None values from a Python list. Some of them are discussed as follows:
Method 1: Naive Method
In the naive method, we iterate through the whole list and append all the filtered, non-None values into a new list, hence ready to be performed with subsequent operations.
Python3
test_list = [ 1 , None , 4 , None , None , 5 , 8 , None ]
print ( "The original list is : " + str (test_list))
res = []
for val in test_list:
if val ! = None :
res.append(val)
print ( "List after removal of None values : " + str (res))
|
Output:
The original list is : [1, None, 4, None, None, 5, 8, None]
List after removal of None values : [1, 4, 5, 8]
Time Complexity: O(n), where n is the length of the input list.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.
Method 2: Using list comprehension
The longer task of using the naive method and increasing line of codes can be done in a compact way using this method. We just check for not None values and construct the new filtered list.
Python3
test_list = [ 1 , None , 4 , None , None , 5 , 8 , None , False ]
print ( "The original list is : " + str (test_list))
res = [i for i in test_list if i is not None ]
print ( "List after removal of None values : " + str (res))
|
Output:
The original list is : [1, None, 4, None, None, 5, 8, None, False]
List after removal of None values : [1, 4, 5, 8, False]
Method 3 : Using filter()
The Python filter() function is the most concise and readable way to perform this particular task. It checks for any None value in list and removes them and form a filtered list without the None values.
Python3
test_list = [ 1 , None , 4 , None , None , 5 , 8 , None , False ]
print ( "The original list is : " + str (test_list))
res = list ( filter ( lambda item: item is not None , test_list))
print ( "List after removal of None values : " + str (res))
|
Output:
The original list is : [1, None, 4, None, None, 5, 8, None, False]
List after removal of None values : [1, 4, 5, 8, False]
Method 4: Using while loop
Python3
test_list = [ 1 , None , 4 , None , None , 5 , 8 , None ]
print ( "The original list is : " + str (test_list))
res = []
while ( None in test_list):
test_list.remove( None )
print ( "List after removal of None values : " + str (test_list))
|
Output
The original list is : [1, None, 4, None, None, 5, 8, None]
List after removal of None values : [1, 4, 5, 8]
Time Complexity: O(n), where n is length of test_list.
Auxiliary Space: O(m), where m is length of res list.
Method 5: Using Slicing
Here is another approach to remove None values from a list using a while loop and list slicing:
Python3
test_list = [ 1 , None , 4 , None , None , 5 , 8 , None ]
print ( "The original list is:" , test_list)
i = 0
while i < len (test_list):
if test_list[i] is None :
test_list = test_list[:i] + test_list[i + 1 :]
else :
i + = 1
print ( "List after removal of None values:" , test_list)
|
Output
The original list is: [1, None, 4, None, None, 5, 8, None]
List after removal of None values: [1, 4, 5, 8]
The time complexity of this approach is O(n), as the list is traversed once and each element is checked if it is None. The space complexity is O(1), as the same list is used for storing the result and no additional space is required.
Method #6: Using itertools.filterfalse()
Python3
import itertools
test_list = [ 1 , None , 4 , None , None , 5 , 8 , None , False ]
print ( "The original list is : " + str (test_list))
res = list (itertools.filterfalse( lambda item: not item , test_list))
print ( "List after removal of None values : " + str (res))
|
Output
The original list is : [1, None, 4, None, None, 5, 8, None, False]
List after removal of None values : [1, 4, 5, 8]
Time Complexity: O(N)
Auxiliary Space: O(1)
Method #7: Using remove()
Python3
test_list = [ 1 , None , 4 , None , None , 5 , 8 , None ]
print ( "The original list is : " + str (test_list))
while None in test_list:
test_list.remove( None )
print ( "List after removal of None values : " + str (test_list))
|
Output
The original list is : [1, None, 4, None, None, 5, 8, None]
List after removal of None values : [1, 4, 5, 8]
Time Complexity:O(N)
Auxiliary Space :O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...