How to Delete the True Values in a Python List?


In Python, lists are perhaps of the most regularly used datum structures that permit us to store different elements. Now and then, we might experience circumstances where we really want to remove explicit elements from a list. In this article, we will discuss five distinct methods to remove the True values from a Python list. We will examine the algorithm, give code examples, show the result, and close with a correlation of the methods.

Introduction

Consider a situation where we have a list containing a combination of boolean type and different information types, and we need to remove all events of True from that list. Python furnishes us with a few ways to deal with accomplish this errand. We will investigate 6 distinct methods to take special care of different inclinations and use cases.

Let us 1st see an example input and output.

Input

[True, 2, False, True, 4, True, 6, True]

Output

[2, False, 4, 6]

Explanation: True is a boolean value representing the logical True state. There are 2 built-in constant boolean which are True and False.

Our goal is to remove the True values from the list and let all other values remain. In the input above, True occurs at position (not index) 1,4,6,8 which are removed and hence leaving us behind with the rest of the list consisting of the values 2,False,4 and 6.

Method 1: Naive Method

Algorithm

  • Make a function delete_true_naive that accepts an information list as its contention.

  • Instate a list variable to 0.

  • While the list is not exactly the length of the info list:

  • Assuming the element at the ongoing list is True, remove it using the pop() method.

  • In any case, increase the list by 1.

  • Return the changed information list

Example

def delete_true_naive(input_list):
    index = 0
    while index < len(input_list):
        if input_list[index] == True:
            input_list.pop(index)
        else:
            index += 1
    return input_list
sample_list = [True, 2, False, True, 4, True, 6, True]
print("Naive Method Output:", delete_true_naive(sample_list.copy()))

Output

Naive Method Output: [2, False, 4, 6]

Method 2: Using List Comprehension

Algorithm

  • Make a capability delete_true_list_comprehension that acknowledges a data list as its conflict.

  • Use list perception to make one more rundown that integrates only those components from the data list that are not identical to Valid.

  • Return the new rundown.

Example

def delete_true_list_comprehension(input_list):
    return [element for element in input_list if element != True]

sample_list = [True, 2, False, True, 4, True, 6, True]
print("Using list comprehension output:", delete_true_list_comprehension(sample_list.copy()))

Output

Using list comprehension output: [2, False, 4, 6]

Strategy 3: Utilizing filter() function with a Lambda function

Algorithm

  • Make a capability delete_true_filter that acknowledges a data list as its dispute.

  • Utilize the channel() capability close by a lambda capability to filter through the components from the data list that are not comparable to Valid.

  • Convert the isolated result back to a rundown utilizing the rundown() capability.

  • Return the filtered list.

Example

def delete_true_filter(input_list):
    return list(filter(lambda x: x != True, input_list))
sample_list = [True, 2, False, True, 4, True, 6, True]
print("Using filter() Result:", delete_true_filter(sample_list.copy()))

Output

Using filter() Result: [2, False, 4, 6]

Strategy 4: Utilizing remove()

Algorithm

  • Make a function delete_true_remove that acknowledges a data list as its conflict.

  • Utilize some time circle to rehash for whatever length of time Clear is accessible in the data list.

  • Inside the circle, utilize the eliminate() technique to eliminate the essential occasion of Valid from the rundown.

  • Return the changed data list.

Example

def delete_true_remove(input_list):
    while True in input_list:
        input_list.remove(True)
    return input_list 
sample_list = [True, 2, False, True, 4, True, 6, True]
print("Using remove() Result:", delete_true_remove(sample_list.copy()))

Output

Using remove() Result: [2, False, 4, 6]

Method 5: Using List Comprehension and enumerate() Function

Algorithm

  • Make a function delete_true_enum that accepts an information list as its contention.

  • Use list comprehension alongside the count() function to emphasize through the information list while monitoring both the list and value of every element.

  • Remember just those elements for the new list for which the value isn't equivalent to True.

  • Return the new list.

Example

def delete_true_enum(input_list):
    return [value for index, value in enumerate(input_list) if value != True]
# Sample list
sample_list = [True, 2, False, True, 4, True, 6, True]
# Output
print("Using List Comprehension and enumerate() Output:", delete_true_enum(sample_list.copy()))

Output

Using List Comprehension and enumerate() Output: [2, False, 4, 6]

Method 6: Using itertools.filterfalse()

The itertools module is commonly used for handling iteration scenarios and the filterfalse is used to return values that are return a false value from a specified function. It takes in 2 inputs: function; object to iterate; The value that filterfalse returns is are those that return false after being passed to the function (passed as argument).

Algorithm

  • We first and foremost import the filterfalse() function from the itertools module.

  • The next step is to create a function delete_true_itertools that accepts an input list as its argument.

  • Then we use filterfalse() with a lambda function to filter out the True values from the list.

  • After that we convert the filtered result back to a list using the list() function.

  • Lastly return the new list without True values.

Example

from itertools import filterfalse

def delete_true_itertools(input_list):
    return list(filterfalse(lambda x: x == True, input_list))

sample_list = [True, 2, False, True, 4, True, 6, True]
print("Using itertools.filterfalse() Result:", delete_true_itertools(sample_list.copy()))

Output

Using itertools.filterfalse() Result: [2, False, 4, 6]

Conclusion

In this post, we've learned about 6 different ways to get rid of True values from a list in Python and they include the naive approach, list comprehension, filter() with a lambda function, remove function, and list comprehension combined with enumerate(). Lastly the itertools modules’ filterfalse function is also among the available ways. The best approach will depend on the particular use case and performance demands, although for smaller lists, all methods may remove True values efficiently.

Updated on: 29-Aug-2023

148 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements