Python – False values Frequency
Last Updated :
13 Apr, 2023
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.
Similar Reads
Python - Keys Values equal frequency
Given a dictionary, count instances where keys are equal to values. Input : test_dict = {5:5, 8:9, 7:8, 1:2, 10:10, 4:8} Output : 2 Explanation : At 2 instances, keys are equal to values.Input : test_dict = {5:4, 8:9, 7:8, 1:2, 10:10, 4:8} Output : 1 Explanation : At 1 instance, key is equal to valu
4 min read
Python - Values frequencies of key
Sometimes, while working with Python dictionary lists, one can have a problem in which we require to find the frequency of all the values occurring in a specific key in dictionary list. This can have application across many domains including web development. Let's discuss certain ways in which this
7 min read
Python - Assign Frequency to Tuples
Given tuple list, assign frequency to each tuple in list. Input : test_list = [(6, 5, 8), (2, 7), (6, 5, 8), (9, ), (2, 7)] Output : [(6, 5, 8, 2), (2, 7, 2), (9, 1)] Explanation : (2, 7) occurs 2 times, hence 2 is append in tuple.Input : test_list = [(2, 7), (2, 7), (6, 5, 8), (9, ), (2, 7)] Output
8 min read
Python - Dictionary List Values Frequency
Sometimes, while working with Python dictionaries, we can have a problem in which we need to perform the task of computing frequency of all the values in dictionary values lists. This is quite common problem and can have use cases in many domains. Let's discuss certain ways in which this task can be
6 min read
Python - Values Frequency Index List
Sometimes, while working with Python tuples, we can have a problem in which we need to extract the frequency of each value in tuple. This has been solved earlier. We can have a modification in which we need to create list in which index represents the key and value of it represents the frequency of
4 min read
Python - Keys Frequency with Value atmost K
Sometimes, while working with Python dictionaries, we can come across a problem in which we have a particular value, and we need to find frequency if itâs occurrence and value is atmost K. Letâs discuss certain ways in which this problem can be solved. Method #1: Using loop This problem can be solve
5 min read
Python | Tuple Column element frequency
In Python, we need to handle various forms of data and one among them is a list of tuples in which we may have to perform any kind of operation. This particular article discusses the ways of finding the frequency of the Kth element in the list of tuples. Letâs discuss certain ways in which this can
5 min read
Truthy vs Falsy Values in Python
In this article, we will see about the concept of Truthy and Falsy values in Python and also see how to determine a value is Truthy or Falsy by using bool() built-in Python function. Now, Let's start with the following two codes: C/C++ Code number = 7 if number: print(number) Output: 7 Let's change
3 min read
Python - Associated Values Frequencies in Dictionary
Sometimes, while working with dictionaries, we can have problem in which we need to compute the values associated to each value in dictionary in records list. This kind of problem is peculiar, but can have application in development domains. Lets discuss certain way in which this task can be perform
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