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 task can be performed.
Method #1: Using loop This is brute force method to solve this problem. In this, we iterate for each key and tag it as false if we find first occurrence of false value and break from loop.
Python3
# Python3 code to demonstrate working of # Test Boolean Value of Dictionary # Using loop # initializing dictionary test_dict = { 'gfg' : True , 'is' : False , 'best' : True } # printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # Test Boolean Value of Dictionary # Using loop res = True for ele in test_dict: if not test_dict[ele]: res = False break # printing result print ( "Is Dictionary True ? : " + str (res)) |
The original dictionary is : {'gfg': True, 'is': False, 'best': True} Is Dictionary True ? : False
Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(1), as we are only using a constant amount of extra space to store the result and loop variables.
Method #2 : Using all() + values()
Step-by-step approach:
- Initialize a dictionary test_dict with keys and boolean values.
- Print the original dictionary using the print() function.
- Apply the all() function to the values of the dictionary by calling test_dict.values() as an argument to all(). The all() function returns True if all the elements in the given iterable are true, and False otherwise.
- Store the result of all(test_dict.values()) in a variable res.
- Print the result of the all() function using the print() function along with a message. If the all() function returns True, then the dictionary has all values as True, and if it returns False, then at least one value in the dictionary is False.
Below is the implementation of the above approach:
Python3
# Python3 code to demonstrate working of # Test Boolean Value of Dictionary # Using all() + values() # initializing dictionary test_dict = { 'gfg' : True , 'is' : False , 'best' : True } # printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # Test Boolean Value of Dictionary # Using all() + values() res = all (test_dict.values()) # printing result print ( "Is Dictionary True ? : " + str (res)) |
The original dictionary is : {'gfg': True, 'is': False, 'best': True} Is Dictionary True ? : False
Time Complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary Space: O(1). This is because the only additional space used is for the variable res.
Method #3 : Using in operator and values() method
Python3
# Python3 code to demonstrate working of # Test Boolean Value of Dictionary # initializing dictionary test_dict = { 'gfg' : True , 'is' : False , 'best' : True } # printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # Test Boolean Value of Dictionary res = True x = list (test_dict.values()) if False in x: res = False # printing result print ( "Is Dictionary True ? : " + str (res)) |
The original dictionary is : {'gfg': True, 'is': False, 'best': True} Is Dictionary True ? : False
Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(n), where n is the number of key-value pairs in the dictionary.
Method #4 : Using values() and count() methods
Python3
# Python3 code to demonstrate working of # Test Boolean Value of Dictionary # initializing dictionary test_dict = { 'gfg' : True , 'is' : False , 'best' : True } # printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # Test Boolean Value of Dictionary res = True x = list (test_dict.values()) if (x.count( False )> = 1 ): res = False # printing result print ( "Is Dictionary True ? : " + str (res)) |
The original dictionary is : {'gfg': True, 'is': False, 'best': True} Is Dictionary True ? : False
Time complexity: O(n), where n is the number of values in the dictionary.
Auxiliary space: O(1). The code uses a constant amount of additional space to store the boolean result and the list of dictionary values.
Method 5: using a list comprehension
In this implementation, we are using a list comprehension to extract the values from the dictionary and passing the result to the all() function. This approach is similar to the previous one but provides a more concise way of extracting the values from the dictionary.
Below is the implementation:
Python3
# initializing dictionary test_dict = { 'gfg' : True , 'is' : False , 'best' : True } # printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # Test Boolean Value of Dictionary # Using list comprehension res = all ([value for value in test_dict.values()]) # printing result print ( "Is Dictionary True ? : " + str (res)) |
The original dictionary is : {'gfg': True, 'is': False, 'best': True} Is Dictionary True ? : False
Time complexity: O(n), where n is the number of keys in the dictionary. The reason for this is that the implementation needs to iterate over all the values in the dictionary in order to check if they are all True.
Auxiliary space: O(1)
Method 6: Using any() + values()
Use the any() function along with the values() method to check if any value in the dictionary is False. If there is any False value, then the dictionary is not true.
STEP BY STEP APPROACH :
- Import the Dict type from the typing module. This is used to specify the type of the input dictionary to the test_dict_bool function.
- Define the test_dict_bool function that takes a dictionary as input and returns a boolean value.
- Inside the function, create a generator expression that iterates through the values of the dictionary using the values() method.
- For each value in the dictionary, use the is operator to test if the value is explicitly equal to False.
- Wrap the generator expression inside the any() function, which returns True if any of the values in the generator expression are True, and False otherwise.
- Invert the result of the any() function using the not keyword to get True if there are no False values in the dictionary, and False otherwise.
- Return the final boolean value.
- In the main part of the program, create a dictionary called test_dict with boolean values.
- Print the original dictionary using the print() function.
- Call the test_dict_bool function with the test_dict dictionary as input.
- Print the result using the print() function. The result will be a boolean value indicating whether the dictionary has only boolean values that are True.
Python3
# Python3 code to demonstrate working of # Test Boolean Value of Dictionary # Using any() + values() # importing the required module from typing import Dict # function to test boolean value of dictionary def test_dict_bool(test_dict: Dict [ str , bool ]) - > bool : # We use the `any()` function to check if any value in the dictionary is `False`. # If there is any `False` value, then the dictionary is not true. # We iterate through the dictionary values using the `values()` method, and use # a generator expression to create a Boolean value for each value in the dictionary. # We use the `not` keyword to invert each Boolean value, so that `False` becomes `True` # and `True` becomes `False`. # We pass the resulting Boolean values to `any()`, which returns `True` if any of the # values in the generator expression are `True`, and `False` otherwise. # We use the `not` keyword to invert the result, so that `True` becomes `False` if any # value in the dictionary is `False`, and `True` otherwise. res = not any (val is False for val in test_dict.values()) return res # driver code if __name__ = = '__main__' : # initializing dictionary test_dict = { 'gfg' : True , 'is' : False , 'best' : True } # printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # Test Boolean Value of Dictionary # Using any() + values() res = test_dict_bool(test_dict) # printing result print ( "Is Dictionary True ? : " + str (res)) |
The original dictionary is : {'gfg': True, 'is': False, 'best': True} Is Dictionary True ? : False
Time complexity: O(n), where n is the number of keys in the dictionary.
Auxiliary space: O(1)
Method 7: Using reduce() function from functools module
Step-by-step approach:
- Import the functools module to use the reduce() function.
- Initialize the dictionary ‘test_dict‘ with the given key-value pairs.
- Use the reduce() function to apply the AND operation on all the boolean values of the dictionary using a lambda function. The lambda function takes two arguments, x and y, and returns their AND value. The reduce() function applies the lambda function to each pair of elements in the sequence, starting from the first two elements.
- The initial value of the sequence is set to True, as the AND operation with True and any other boolean value will always result in the boolean value itself.
- The final result is stored in the variable ‘res‘.
- Print the original dictionary and the result of the Boolean value of the dictionary.
Below is the implementation of the above approach:
Python3
import functools # initializing dictionary test_dict = { 'gfg' : True , 'is' : False , 'best' : True } # printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # Test Boolean Value of Dictionary # Using reduce() function res = functools. reduce ( lambda x, y: x and y, test_dict.values(), True ) # printing result print ( "Is Dictionary True ? : " + str (res)) |
The original dictionary is : {'gfg': True, 'is': False, 'best': True} Is Dictionary True ? : False
Time complexity: The time complexity of this approach is O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: The space complexity of this approach is O(1), as we are not using any additional data structure to store the intermediate results.