Python – Truth values deletion in List
Last Updated :
22 Apr, 2023
Due to the upcoming of Machine Learning, focus has now moved on handling the values than ever before, the reason behind this is that it is the essential step of data preprocessing before it is fed into further techniques to perform. Hence removal of values in essential, be it None, or sometimes Truth, and knowledge of it is a must. Lets discuss certain ways in which this is achieved.
Method #1 : Naive Method In naive method, we iterate through whole list and append all the filtered, None values into a new list, hence ready to be performed with subsequent operations.
Python3
test_list = [ 1 , None , 4 , None , False , 5 , 8 , False ]
print ( "The original list is : " + str (test_list))
res = []
for val in test_list:
if not val:
res.append(val)
print ( "List after removal of Truth values : " + str (res))
|
Output
The original list is : [1, None, 4, None, False, 5, 8, False]
List after removal of Truth values : [None, None, False, False]
Time Complexity: O(n),The above code iterates through the list once, hence the time complexity is linear, i.e. O(n).
Space Complexity: O(n),The algorithm uses an additional list to store the result, thus consuming linear space which is O(n).
Method #2 : Using list comprehension The longer task of using the naive method and increasing line of codes can be done in compact way using this method. We just check for None values and construct the new filtered list.
Python3
test_list = [ 1 , None , 4 , None , False , 5 , 8 , False ]
print ( "The original list is : " + str (test_list))
res = [i for i in test_list if not i]
print ( "List after removal of Truth values : " + str (res))
|
Output
The original list is : [1, None, 4, None, False, 5, 8, False]
List after removal of Truth values : [None, None, False, False]
Time Complexity: O(n), where n is the length of the input list. This is because we’re using list comprehension which has a time complexity of O(n) in the worst case.
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 #3 : Using filter() function with a lambda function
Another approach to remove truth values from a list in Python is by using the filter() function with a lambda function that checks for truth values.
Example:
Python3
test_list = [ 1 , None , 4 , None , False , 5 , 8 , False ]
res = list ( filter ( lambda x: x is not None and x is not False , test_list))
print ( "List after removal of Truth values :" , res)
|
Output
List after removal of Truth values : [1, 4, 5, 8]
Time complexity: O(n) as it is iterating through the list once.
Auxiliary Space: O(n) as it is creating a new list with filtered values.
Method #4 : Using remove ():
Python3
test_list = [ 1 , None , 4 , None , False , 5 , 8 , False ]
print ( "The original list is : " + str (test_list))
for val in [ None , False ]:
while val in test_list:
test_list.remove(val)
res = test_list
print ( "List after removal of Truth values : " + str (res))
|
Output
The original list is : [1, None, 4, None, False, 5, 8, False]
List after removal of Truth values : [1, 4, 5, 8]
Time complexity: O(n^2)
Auxiliary Space: O(1)
Method #5: Using List Comprehension and Enumerate() function: In this method, we create a new list that only checks for true values in a boolean context and the enumerate() function is used to add an index to all the elements in the list that references the original list using the test_list.
Python3
test_list = [ 1 , None , 4 , None , False , 5 , 8 , False ]
print ( "The original list is : " + str (test_list))
test_list = [test_list[i] for i, val in enumerate (test_list) if val]
print ( "List after removal of Truth values : " + str (test_list))
|
Output
The original list is : [1, None, 4, None, False, 5, 8, False]
List after removal of Truth values : [1, 4, 5, 8]
Time complexity: O(n), where n is the length of the input list test_list
Auxiliary Space: O(k), where k represents the number of elements in the input list that are considered True.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...