To count the number of False values in a list in Python, the easiest way is with list comprehension and the Python len() function.
lst = [True, False, True, False]
count = len([val for val in lst if val == False])
print(count)
#Output:
2
If you have a list which has numbers in it, you will have to be careful since False is equal to 0. In this case, you should also check if the variable type is a bool.
lst = [True, False, True, False, 1, 2, 3]
count = len([val for val in lst if val == False and type(val) == type(False)])
print(count)
#Output:
2
You can also use sum() if your list only has boolean values.
lst = [True, False, True, False]
print(sum(lst))
#Output:
2
When working with collections of data, the ability to easily summarize and get statistics about the collection is valuable.
One such case is if you want to count the number of False values in a list.
To count the False values in a list in Python, the easiest way is with list comprehension and the Python len() function. You can use an if statement to get if the value is False or not.
Below is a simple example showing you how to count the number of Falses in a list using Python.
lst = [True, False, True, False]
count = len([val for val in lst if val == False])
print(count)
#Output:
2
If you have a list which has numbers in it, you will have to be careful since False is equal to 0. In this case, you should also check if the variable type is a bool.
lst = [True, False, True, False, 0, 1, 2]
count = len([val for val in lst if val == False and type(val) == type(False)])
print(count)
#Output:
2
Get Count of Number of True in List Using Python
If you want to go the other way and get the count of number of True in a list using Python, you can just make a simple adjustment to the code above.
All you need to do is change the if statement.
Below is an example showing you how to count the number of True values in a list using Python.
lst = [True, False, True, False]
count = len([val for val in lst if val == True])
print(count)
#Output:
2
Hopefully this article has been useful for you to learn how to count the number of False values in a list using Python.