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
# Python3 code to demonstrate
# Truth values deletion in List
# using naive method
# initializing list
test_list = [1, None, 4, None, False, 5, 8, False]
# printing original list
print ("The original list is : " + str(test_list))
# using naive method
# Truth values deletion in List
res = []
for val in test_list:
if not val:
res.append(val)
# printing result
print ("List after removal of Truth values : " + str(res))
OutputThe 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
# Python3 code to demonstrate
# Truth values deletion in List
# using list comprehension
# initializing list
test_list = [1, None, 4, None, False, 5, 8, False]
# printing original list
print ("The original list is : " + str(test_list))
# using list comprehension
# Truth values deletion in List
res = [i for i in test_list if not i]
# printing result
print ("List after removal of Truth values : " + str(res))
OutputThe 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
# Python program to remove truth values from a list
test_list = [1, None, 4, None, False, 5, 8, False]
# Using filter() function with a lambda function
res = list(filter(lambda x: x is not None and x is not False, test_list))
print("List after removal of Truth values :", res)
#This code is contributed by Edula Vinay Kumar Reddy
OutputList 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))
#This code is contributed by Jyothi pinjala.
OutputThe 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
# initializing list
test_list = [1, None, 4, None, False, 5, 8, False]
# printing original list
print("The original list is : " + str(test_list))
# using List Comprehension and Enumerate() function
# Truth values deletion in List
test_list = [test_list[i] for i, val in enumerate(test_list) if val]
# printing result
print("List after removal of Truth values : " + str(test_list))
OutputThe 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.
Similar Reads
Python | Get indices of True values in a binary list Boolean lists are often used by developers to check for True values during hashing. The boolean list is also used in certain dynamic programming paradigms in dynamic programming. Let's discuss certain ways to get indices of true values in a list in Python. Method #1 : Using enumerate() and list comp
9 min read
Python | Values till False element Many a times we require to find the first occurring False number to begin the processing with. This has mostly use case in Machine Learning domain in which we require to process data excluding None or 0 values. Letâs discuss certain ways in which this can be performed. Method #1 : Using next() + enu
4 min read
Python - Odd elements removal in List Due to the upcoming of Machine Learning, focus has now moved on handling the certain 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 certain values in essential and knowledge
5 min read
Python | Clearing list as dictionary value Clearing a list is a common problem and solution to it has been discussed many times. But sometimes, we don't have a native list but list is a value to dictionary key. Clearing it is not as easy as clearing an original list. Let's discuss certain ways in which this can be done. Method #1: Using loop
6 min read
Python - Remove elements at Indices in List Given List, remove all the elements present in the indices list in Python. Input : test_list = [5, 6, 3, 7, 8, 1, 2, 10], idx_list = [2, 4, 5]Â Output : [5, 6, 7, 2, 10]Â Explanation : 3, 6, and 1 has been removed. Input : test_list = [5, 6, 3, 7, 8, 1, 2, 10], idx_list = [2]Â Output : [5, 6, 7, 8,
7 min read
Python | Remove given element from the list Given a list, write a Python program to remove the given element (list may have duplicates) from the given list. There are multiple ways we can do this task in Python. Let's see some of the Pythonic ways to do this task. Example: Input: [1, 8, 4, 9, 2] Output: [1, 8, 4, 2] Explanation: The Element 9
7 min read
Python - Convert String Truth values to Boolean Converting string truth values like "True" and "False" into their corresponding boolean values in Python is a straightforward yet useful operation. Methods such as dictionaries, direct comparison, and built-in functions offer simple and efficient ways to achieve this transformation for various use c
2 min read
Python | Segregate True and False value indices Sometimes, while working with Python lists, we can have a problem in which we have boolean list and require to have indices of True and False values separately. This can have a possible use in Data Science domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop
6 min read
Python - Truncate a list We are given a list we need to truncate list. For example, we are having a list a = [1, 2, 3, 4, 5] we need to truncate (removing 2 elements from behind) list so that the output should be [1,2,3].Using List SlicingList slicing allows truncating a list by specifying the starting and ending indices.Py
2 min read
Python - Remove duplicate values in dictionary Sometimes, while working with Python dictionaries, we can have problem in which we need to perform the removal of all the duplicate values of dictionary, and we are not concerned if any key get removed in the process. This kind of application can occur in school programming and day-day programming.
8 min read