Python | Count true booleans in a list
Last Updated :
30 Mar, 2023
Given a list of booleans, write a Python program to find the count of true booleans in the given list.
Examples:
Input : [True, False, True, True, False]
Output : 3
Input : [False, True, False, True]
Output : 2
Method #1: Using List comprehension One simple method to count True booleans in a list is using list comprehension.
Python3
# Python3 program to count True booleans in a list
def count(lst):
return sum(bool(x) for x in lst)
# Driver code
lst = [True, False, True, True, False]
print(count(lst))
Method #2 : Using sum()
Python3
# Python3 program to count True booleans in a list
def count(lst):
return sum(lst)
# Driver code
lst = [True, False, True, True, False]
print(count(lst))
A more robust and transparent method to use sum is given below.
Python3
def count(lst):
return sum(1 for x in lst if x)
Method #3 : count() method
Python3
# Python3 program to count True booleans in a list
def count(lst):
return lst.count(True)
# Driver code
lst = [True, False, True, True, False]
print(count(lst))
Method #4 : filter()
Python3
# Python3 program to count True booleans in a list
def count(lst):
return len(list(filter(None, lst)))
# Driver code
lst = [True, False, True, True, False]
print(count(lst))
Method #5 : Using for loop
Python3
# Python3 program to count True booleans in a list
def count(lst):
c=0
for i in lst:
if(i==True):
c+=1
return c
# Driver code
lst = [True, False, True, True, False]
print(count(lst))
Method #6 : Using lambda function,len() methods
Python3
# Python3 program to count True booleans in a list
def count(lst):
return len(list(filter(lambda x: x == True, lst)))
# Driver code
lst = [True, False, True, True, False]
print(count(lst))
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 7: using operator.countOf() method
Python3
# Python3 program to count True booleans in a list
import operator as op
def count(lst):
return op.countOf(lst, True)
# Driver code
lst = [True, False, True, True, False]
print(count(lst))
Time Complexity: O(N)
Auxiliary Space : O(1)
Method #8:Using itertools.filterfalse() method
Python3
# Python3 program to count True booleans in a list
import itertools
def count(lst):
return len(list(itertools.filterfalse(lambda x: x == False, lst)))
# Driver code
lst = [True, False, True, True, False]
print(count(lst))
Time Complexity: O(N)
Auxiliary Space : O(1)
Method #9: Using numpy:
Algorithm:
- Convert the given list to a NumPy array using np.array() function.
- Count the number of non-zero elements in the array using np.count_nonzero() function.
- Return the count of non-zero elements as the output.
Python3
import numpy as np
def count(lst):
arr = np.array(lst)
return np.count_nonzero(arr)
lst = [True, False, True, True, False]
print(count(lst))
#This code is contributed by Rayudu
Output:
3
Time complexity:
Converting the list to a NumPy array takes O(n) time, where n is the length of the list.
Counting the number of non-zero elements in the array takes O(n) time as well.
Therefore, the overall time complexity of the count function is O(n), where n is the length of the input list.
Auxiliary Space:
Converting the list to a NumPy array requires O(n) space as the NumPy array needs to store the same number of elements as the input list.
Counting the number of non-zero elements requires O(1) space as it only needs to store a single integer.
Therefore, the overall space complexity of the count function is O(n), where n is the length of the input list.
Similar Reads
Python - False indices in a boolean list
Boolean lists are often used by the developers to check for False values during hashing. These have many applications in developers daily life. Boolean list is also used in certain dynamic programming paradigms in dynamic programming. Also in Machine Learning preprocessing of values. Lets discuss ce
7 min read
Python | Contiguous Boolean Range
Sometimes, we come across a problem in which we have a lot of data in a list in the form of True and False value, this kind of problem is common in Machine learning domain and sometimes we need to know that at a particular position which boolean value was present. Let's discuss certain ways in which
6 min read
Boolean list initialization - Python
We are given a task to initialize a list of boolean values in Python. A boolean list contains elements that are either True or False. Let's explore several ways to initialize a boolean list in Python.Using List MultiplicationThe most efficient way to initialize a boolean list with identical values i
2 min read
Python - Count elements in tuple list
Sometimes, while working with data in form of records, we can have a problem in which we need to find the count of all the records received. This is a very common application that can occur in Data Science domain. Letâs discuss certain ways in which this task can be performed. Method #1: Using len()
5 min read
Python - Truth values deletion in List
Due to the upcoming of Machine Learning, focus has now moved on handling the values than ever before, the reason behind this is that it is the essential step of data preprocessing before it is fed into further techniques to perform. Hence removal of values in essential, be it None, or sometimes Trut
5 min read
Python | Filter list by Boolean list
Sometimes, while working with a Python list, we can have a problem in which we have to filter a list. This can sometimes, come with variations. One such variation can be filtered by the use of a Boolean list. Let's discuss a way in which this task can be done. Using Numpy to Filter list by Boolean
5 min read
Python | Test for False list
Sometimes, we need to check if a list is completely True of False, these occurrences come more often in testing purposes after the development phase. Hence, having a knowledge of all this is necessary and useful. Lets discuss certain ways in which this can be performed. Method #1: Naive Method In th
8 min read
Python - Step Frequency of elements in List
Sometimes, while working with Python, we can have a problem in which we need to compute frequency in list. This is quite common problem and can have usecase in many domains. But we can atimes have problem in which we need incremental count of elements in list. Let's discuss certain ways in which thi
4 min read
Python - Test Boolean Value of Dictionary
Sometimes, while working with data, we have a problem in which we need to accept or reject a dictionary on the basis of its true value, i.e all the keys are Boolean true or not. This kind of problem has possible applications in data preprocessing domains. Let's discuss certain ways in which this tas
9 min read
Python | Ways to convert Boolean values to integer
Given a boolean value(s), write a Python program to convert them into an integer value or list respectively. Given below are a few methods to solve the above task. Convert Boolean values to integers using int()Â Converting bool to an integer using Python typecasting. Python3 # Initialising Values bo
3 min read