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
# initializing list test_list = [ 1 , None , 4 , None , None , 5 , 8 , None ] # printing original list print ( "The original list is : " + str (test_list)) # using naive method # to remove None values in list res = [] for val in test_list: if val ! = None : res.append(val) # printing result 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
# Python3 code to demonstrate # removing None values in list # using list comprehension # initializing list test_list = [ 1 , None , 4 , None , None , 5 , 8 , None , False ] # printing original list print ( "The original list is : " + str (test_list)) # using list comprehension # to remove None values in list res = [i for i in test_list if i is not None ] # printing result 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
# initializing list test_list = [ 1 , None , 4 , None , None , 5 , 8 , None , False ] # printing original list print ( "The original list is : " + str (test_list)) # using filter() # to remove None values in list res = list ( filter ( lambda item: item is not None , test_list)) # printing result 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
#Python program to remove None values from list # initializing list test_list = [ 1 , None , 4 , None , None , 5 , 8 , None ] # printing original list print ( "The original list is : " + str (test_list)) # using naive method # to remove None values in list res = [] while ( None in test_list): test_list.remove( None ) # printing result print ( "List after removal of None values : " + str (test_list)) |
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) #This code is contributed by Edula Vinay Kumar Reddy |
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 # initializing list test_list = [ 1 , None , 4 , None , None , 5 , 8 , None , False ] # printing original list print ( "The original list is : " + str (test_list)) # using filter() # to remove None values in list res = list (itertools.filterfalse( lambda item: not item , test_list)) # printing result print ( "List after removal of None values : " + str (res)) |
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
# initializing list test_list = [ 1 , None , 4 , None , None , 5 , 8 , None ] # printing original list print ( "The original list is : " + str (test_list)) # using remove method to remove None values in list while None in test_list: # removing None from list using remove method test_list.remove( None ) # printing result print ( "List after removal of None values : " + str (test_list)) #This code is contributed by Vinay Pinjala. |
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)