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)) |
3
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)) |
3
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)) |
3
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)) |
3
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)) |
3
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)) |
3
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)) |
3
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)) |
3
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.