Count True & False in List in Python (3 Examples)
In this Python article, you’ll learn how to count the True and False values in a list of booleans.
The page will contain the following contents:
Let’s go straight to the code!
Creation of Data Sample
We will use the data below as a basis for this Python tutorial.
my_list = [True, False, True, False, True, False, False, False, True] print(my_list) # [True, False, True, False, # True, False, False, False, True]
As shown in the previous output, our example data is a boolean list that contains True and False values. Let’s see how to count every True and False values in our list!
Example 1: Use count() Method to Get Number of True and False Values in List
This first example explains how to use the count() method to get the number of True and False values in our list. Consider the code below to count the True values in our list.
my_list.count(True) # 4
As expected, the count() method counts four True values in our boolean list. We can now count the False values using the same procedure.
my_list.count(False) # 5
As shown, there are five False values in my_list.
Example 2: Use sum() and len() Functions to Get Number of True and False Values in List
Alternatively, we can use the sum() function to count the True values in our list. Please take a look at the following code!
sum(my_list) # 4
Just as supposed to, the sum() function counts four occurrences of True in my_list.
But what about the False values? To get the number of False occurrences in a list, we can just combine the sum() and the len() functions.
len(my_list) - sum(my_list) # 5
The len() function extracts the number of total elements in our list, and subtracting the True values from the total number of elements gives us the number of False values in my_list, which is five.
Example 3: Use operator.countOf() Function to Get Number of True and False Values in List
We can also use the CountOf() function to count the number of True and False values in a list. To use it, first, we will import the operator module.
import operator as op
Now, we will plug our list and the ‘True’ statement into op.Countof() to count the True values.
op.countOf(my_list, True) # 4
Great! The CountOf() function counts four True values in my_list. Let’s apply this function to the False values in our list as well!
op.countOf(my_list, False) # 5
Again, as expected, we obtained five False values in my_list 🙂
Video, Further Resources & Summary
Do you need more explanations on how to count the number of True and False values in a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
The YouTube video will be added soon.
Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe:
- Count Occurrences of a Specific Item in List in Python
- Access List Index in for Loop in Python
- Convert List to Range in Python
- Learn Python
- Count Elements Using List Comprehension in Python
This post has shown how to count the number of True and False values in a list in Python. Please let me know in the comments, in case you have additional questions.
This page was created in collaboration with Paula Villasante Soriano. Please have a look at Paula’s author page to get further information about her academic background and the other articles she has written for Statistics Globe.
Statistics Globe Newsletter