How to Count the Number of True in a Python List (5 Easy Ways)

πŸ’‘ Problem Formulation: A common task involves counting the occurrences of a specific condition within a list. We want to count how many True values are present in a list.

We’ll explore several approaches to efficiently perform this operation next. πŸ‘‡

Method 1: Using the sum() Function

One of the simplest methods to count the number of True values in a list is by utilizing Python’s built-in sum() function. Since Python internally represents True as 1 and False as 0, summing over the list directly gives us the count of True values.

bool_list = [True, False, True, True, False]
true_count = sum(bool_list)
print(true_count)  
# Output: 3

The code defines a list of boolean values and then applies the sum() function to it. The sum function iterates over the boolean list, effectively treating True as 1 and False as 0, adding them up to return the total number of True values.

Method 2: Using a for Loop

If you prefer a more explicit method, you can iterate through the list with a for loop and manually count the number of True values.

bool_list = [True, False, True, True, False]
true_count = 0
for value in bool_list:
    if value:
        true_count += 1
print(true_count)  
# Output: 3

We initialize a counter to 0. Then, we loop through each element in the list, incrementing our counter each time we encounter a True value.

Method 3: Using List Comprehension

List comprehension is a concise way to process all items in a list. We can combine this with len() to count True values.

bool_list = [True, False, True, True, False]
true_count = len([b for b in bool_list if b])
print(true_count)
# Output: 3

This snippet involves creating a new list that only contains the True values from the original list and then calculating the length of the new list. The list comprehension filters out all False values, only passing True.

Method 4: Using filter() Function

The filter() function can be used to filter out all non-True values and leave us with a list-like object of True values whose length we can measure.

bool_list = [True, False, True, True, False]
true_count = len(list(filter(lambda x: x, bool_list)))
print(true_count)  # Output: 3

The filter() function takes a lambda function that returns the item if it’s True, effectively filtering out all False values. We then convert the filter object to a list and use len() to count the True values.

Method 5: Using collections.Counter

The Counter class from the collections module can count the frequency of each element in the iterable, including Boolean values.

from collections import Counter
bool_list = [True, False, True, True, False]
true_count = Counter(bool_list)[True]
print(true_count)  
# Output: 3

The Counter object creates a dictionary-like object that maps elements to the number of occurrences. By accessing the True key in this dictionary, we obtain the count of True elements in the list.

Bonus One-Liner Method 6: Using Generator Expression with sum()

You can also pass a generator expression directly to the sum() function for a concise one-liner solution.

bool_list = [True, False, True, True, False]
true_count = sum(1 for elem in bool_list if elem)
print(true_count)  # Output: 3

The generator expression (1 for elem in bool_list if elem) yields a 1 for each True in the list. The sum() function then adds these up to get the final count, making it another efficient one-liner.


Feel free to join my free email academy for fun and learning: