How to Delete the True Values in a Python List?

In Python, lists are one of the most commonly used data structures that allow us to store various elements. Sometimes, we may encounter situations where we need to remove specific boolean values from a list. In this article, we will explore six different methods to remove True values from a Python list.

Problem Statement

Consider a list containing a mixture of boolean values and other data types, and we need to remove all occurrences of True from that list. Let's see an example ?

Input:

sample_list = [True, 2, False, True, 4, True, 6, True]
print("Input:", sample_list)
Input: [True, 2, False, True, 4, True, 6, True]

Expected Output: [2, False, 4, 6]

Explanation: The True values at positions 0, 3, 5, and 7 are removed, leaving only the other values in the list.

Method 1: Using List Comprehension

List comprehension provides a clean and readable way to filter elements ?

def delete_true_list_comprehension(input_list):
    return [element for element in input_list if element is not True]

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

Method 2: Using filter() with Lambda Function

The filter() function creates an iterator from elements that satisfy a condition ?

def delete_true_filter(input_list):
    return list(filter(lambda x: x is not True, input_list))

sample_list = [True, 2, False, True, 4, True, 6, True]
result = delete_true_filter(sample_list)
print("Using filter():", result)
Using filter(): [2, False, 4, 6]

Method 3: Using remove() Method

The remove() method deletes the first occurrence of a value. We use a loop to remove all occurrences ?

def delete_true_remove(input_list):
    # Create a copy to avoid modifying the original
    result_list = input_list.copy()
    while True in result_list:
        result_list.remove(True)
    return result_list

sample_list = [True, 2, False, True, 4, True, 6, True]
result = delete_true_remove(sample_list)
print("Using remove():", result)
Using remove(): [2, False, 4, 6]

Method 4: Using itertools.filterfalse()

The filterfalse() function returns elements where the condition is False ?

from itertools import filterfalse

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

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

Method 5: Naive Approach with While Loop

This method manually iterates through the list and removes True values ?

def delete_true_naive(input_list):
    result_list = input_list.copy()
    index = 0
    while index < len(result_list):
        if result_list[index] is True:
            result_list.pop(index)
        else:
            index += 1
    return result_list

sample_list = [True, 2, False, True, 4, True, 6, True]
result = delete_true_naive(sample_list)
print("Naive method:", result)
Naive method: [2, False, 4, 6]

Method 6: Using enumerate() with List Comprehension

This approach combines enumerate() with list comprehension for more control ?

def delete_true_enumerate(input_list):
    return [value for index, value in enumerate(input_list) if value is not True]

sample_list = [True, 2, False, True, 4, True, 6, True]
result = delete_true_enumerate(sample_list)
print("Using enumerate():", result)
Using enumerate(): [2, False, 4, 6]

Performance Comparison

Method Readability Memory Efficiency Best For
List Comprehension High Good Most cases
filter() + Lambda Medium Good Functional programming style
remove() Method Medium Poor In-place modification
itertools.filterfalse() Medium Excellent Large datasets
Naive Approach Low Poor Learning purposes
enumerate() Medium Good When index is needed

Conclusion

List comprehension is generally the most readable and efficient approach for removing True values from a Python list. Use itertools.filterfalse() for memory-efficient operations on large datasets. The remove() method should be avoided for multiple deletions due to performance issues.

Updated on: 2026-03-27T14:00:43+05:30

475 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements