Python – False values Frequency
Checking a number/element by a condition is a common problem one faces and is done in almost every program. Sometimes we also require to get the totals that match the particular condition to have a distinguish which to not match for further utilization like in data Science. Lets discuss certain ways in which we can count False values.
Method #1: Using sum() + generator expression
This method uses the trick of adding 1 to the sum whenever the generator expression returns true. By the time list gets exhausted, summation of count of numbers matching a condition is returned.
Python3
test_list = [ 3 , False , False , 6 , False , 9 ]
print ( "The original list is : " + str (test_list))
res = sum ( 1 for i in test_list if not i)
print ( "The number of False elements: " + str (res))
|
Output
The original list is : [3, False, False, 6, False, 9]
The number of False elements: 3
Time Complexity: O(n) where n is the number of elements in the string list. The sum() + generator expression is used to perform the task and it takes O(n) time.
Auxiliary Space: O(1) additional space needed
Method #2: Using sum() + map()
map() does the task almost similar to the generator expression, difference is just the internal data structure employed by it is different hence more efficient.
Python3
test_list = [ 3 , False , False , 6 , False , 9 ]
print ( "The original list is : " + str (test_list))
res = sum ( map ( lambda i: not i, test_list))
print ( "The number of False elements: " + str (res))
|
Output
The original list is : [3, False, False, 6, False, 9]
The number of False elements: 3
Method #3: Using count() method
This method used count() function to check the count of False values in the list.
Python3
test_list = [ 3 , False , False , 6 , False , 9 ]
print ( "The original list is : " + str (test_list))
res = test_list.count( False )
print ( "The number of False elements: " + str (res))
|
Output
The original list is : [3, False, False, 6, False, 9]
The number of False elements: 3
Time Complexity: O(n)
Auxiliary Space: O(n), where n is length of list.
Method #4: Using Counter() method
Python3
from collections import Counter
test_list = [ 3 , False , False , 6 , False , 9 ]
print ( "The original list is : " + str (test_list))
freq = Counter(test_list)
falseFreq = freq[ False ]
print ( "The number of False elements: " + str (falseFreq))
|
Output
The original list is : [3, False, False, 6, False, 9]
The number of False elements: 3
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5: Using operator.countOf() method
Python3
import operator as op
test_list = [ 3 , False , False , 6 , False , 9 ]
print ( "The original list is : " + str (test_list))
res = op.countOf(test_list, False )
print ( "The number of False elements: " + str (res))
|
Output
The original list is : [3, False, False, 6, False, 9]
The number of False elements: 3
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #6: Using list comprehension
Python3
test_list = [ 3 , False , False , 6 , False , 9 ]
print ( "The original list is : " + str (test_list))
res = len ([i for i in test_list if not i])
print ( "The number of False elements: " + str (res))
|
Output
The original list is : [3, False, False, 6, False, 9]
The number of False elements: 3
# Time Complexity: O(n)
# Auxiliary Space: O(n)
Method #7: Using reduce method.
Algorithm:
1. The `reduce()` function takes two arguments: a function and an iterable.
2. The function argument must take two arguments: an accumulator and an element from the iterable.
3. The `reduce()` function applies the function to the first two elements of the iterable and saves the result as the accumulator.
4. It then applies the function to the accumulator and the next element in the iterable, saving the result as the new accumulator.
5. This process continues until all elements of the iterable have been processed.
6. The final result of the `reduce()` function is the final value of the accumulator.
Python3
from functools import reduce
test_list = [ 3 , False , False , 6 , False , 9 ]
print ( "The original list is : " + str (test_list))
res = reduce ( lambda acc, x: acc + 1 if x = = False else acc, test_list, 0 )
print ( "The number of False elements: " + str (res))
|
Output
The original list is : [3, False, False, 6, False, 9]
The number of False elements: 3
Time Complexity:
– In the worst case, the `reduce()` method must iterate over all elements in the iterable, so the time complexity is O(n), where n is the number of elements in the iterable.
– However, the time complexity of the function argument passed to `reduce()` can vary, depending on the complexity of the calculation being performed.
Space Complexity:
– The `reduce()` method requires constant space overhead, regardless of the size of the iterable.
– The space complexity of the function argument passed to `reduce()` can also vary, depending on the complexity of the calculation being performed.
Last Updated :
13 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...