Python | Count true booleans in a list
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
def count(lst):
return sum ( bool (x) for x in lst)
lst = [ True , False , True , True , False ]
print (count(lst))
|
Method #2 : Using sum()
Python3
def count(lst):
return sum (lst)
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
def count(lst):
return lst.count( True )
lst = [ True , False , True , True , False ]
print (count(lst))
|
Method #4 : filter()
Python3
def count(lst):
return len ( list ( filter ( None , lst)))
lst = [ True , False , True , True , False ]
print (count(lst))
|
Method #5 : Using for loop
Python3
def count(lst):
c = 0
for i in lst:
if (i = = True ):
c + = 1
return c
lst = [ True , False , True , True , False ]
print (count(lst))
|
Method #6 : Using lambda function,len() methods
Python3
def count(lst):
return len ( list ( filter ( lambda x: x = = True , lst)))
lst = [ True , False , True , True , False ]
print (count(lst))
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 7: using operator.countOf() method
Python3
import operator as op
def count(lst):
return op.countOf(lst, True )
lst = [ True , False , True , True , False ]
print (count(lst))
|
Time Complexity: O(N)
Auxiliary Space : O(1)
Method #8:Using itertools.filterfalse() method
Python3
import itertools
def count(lst):
return len ( list (itertools.filterfalse( lambda x: x = = False , lst)))
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))
|
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.
Last Updated :
30 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...