Python – Test if custom keys equal to K in dictionary

Last Updated : 05 Apr, 2023

Given dictionary and custom keys list, check if all those custom keys equals K.

Input  :  test_dict = {“Gfg” : 5, “is” : 8, “Best” : 10, “for” : 10, “Geeks” : 10}, cust_keys = [“is”, “for”, “Geeks”], K = 10

Output : False

Explanation :  “is” is having 8 as value not 10, hence False

Input  :  test_dict = {“Gfg” : 5, “is” : 10, “Best” : 10, “for” : 10, “Geeks” : 10}, cust_keys = [“is”, “for”, “Geeks”], K = 10

Output : True

Explanation :  All keys have 10 as values.

Method #1 : Using loop

This is brute way in which this task can be performed. In this we iterate each custom key and check if all are equal to K by keeping track using boolean variable.

Python3

 `# Python3 code to demonstrate working of ` `# Test if custom keys equal to K in dictionary` `# Using loop`   `# initializing dictionary` `test_dict ``=` `{``"Gfg"` `: ``5``, ``"is"` `: ``8``, ``"Best"` `: ``10``, ``"for"` `: ``8``, ``"Geeks"` `: ``8``}`   `# printing original dictionary` `print``(``"The original dictionary is : "` `+` `str``(test_dict))`   `# initializing custom keys list ` `cust_keys ``=` `[``"is"``, ``"for"``, ``"Geeks"``]`   `# initializing K ` `K ``=` `8`   `# using loop to check for all keys ` `res ``=` `True` `for` `key ``in` `cust_keys:` `    ``if` `test_dict[key] !``=` `K:` `        `  `        ``# break even if 1 value is not equal to K` `        ``res ``=` `False` `        ``break`   `# printing result ` `print``(``"Are all custom keys equal to K : "` `+` `str``(res)) `

Output

```The original dictionary is : {'Gfg': 5, 'is': 8, 'Best': 10, 'for': 8, 'Geeks': 8}
Are all custom keys equal to K : True```

Method #2 : Using all() + generator expression

The combination of above functionalities can be used to solve this problem. In this, we use all() to check for all the values and generator expression performs required iteration.

Python3

 `# Python3 code to demonstrate working of ` `# Test if custom keys equal to K in dictionary` `# Using all() + generator expression`   `# initializing dictionary` `test_dict ``=` `{``"Gfg"` `: ``5``, ``"is"` `: ``8``, ``"Best"` `: ``10``, ``"for"` `: ``8``, ``"Geeks"` `: ``8``}`   `# printing original dictionary` `print``(``"The original dictionary is : "` `+` `str``(test_dict))`   `# initializing custom keys list ` `cust_keys ``=` `[``"is"``, ``"for"``, ``"Geeks"``]`   `# initializing K ` `K ``=` `8`   `# returns true if all elements match K  ` `res ``=` `all``(test_dict[key] ``=``=` `K ``for` `key ``in` `cust_keys)`   `# printing result ` `print``(``"Are all custom keys equal to K : "` `+` `str``(res)) `

Output

```The original dictionary is : {'Gfg': 5, 'is': 8, 'Best': 10, 'for': 8, 'Geeks': 8}
Are all custom keys equal to K : True```

Method #3:  Using Set intersection to check if all the custom keys in the dictionary have the value K

Step-by-step algorithm:

1. Create a set of intersection of cust_keys and test_dict keys.
2. Check if the intersection set is equal to the set of cust_keys.
3. If the intersection set is not equal to the set of cust_keys, return False.
4. If the intersection set is equal to the set of cust_keys, iterate over each key in cust_keys.
5. Check if the value of the key in test_dict is equal to K.
6. If the value of the key in test_dict is not equal to K, return False.
7. If all the values in test_dict for the custom keys are equal to K, return True.

Python3

 `# initialize a dictionary` `test_dict ``=` `{``"Gfg"` `: ``5``, ``"is"` `: ``8``, ``"Best"` `: ``10``, ``"for"` `: ``8``, ``"Geeks"` `: ``8``}`   `# initialize custom keys and K value` `cust_keys ``=` `[``"is"``, ``"for"``, ``"Geeks"``]` `K ``=` `8`   `# print the original dictionary` `print``(``"The original dictionary is : "` `+` `str``(test_dict))`   `# check if all custom keys are present in the dictionary and have value equal to K` `res ``=` `set``(cust_keys).intersection(test_dict.keys()) ``=``=` `set``(cust_keys) ``and` `all``(test_dict[key] ``=``=` `K ``for` `key ``in` `cust_keys)`   `# print the result` `print``(``"Are all custom keys equal to K : "` `+` `str``(res))`

Output

```The original dictionary is : {'Gfg': 5, 'is': 8, 'Best': 10, 'for': 8, 'Geeks': 8}
Are all custom keys equal to K : True```

Complexity Analysis:

Time Complexity:  O(n), where n is the length of the cust_keys list.

Auxiliary Space: O(m), where m is the number of keys in the test_dict dictionary.

Method #4: Using the filter() function and a lambda function:

• Initialize the integer variable K with the value to be compared.
• Define a lambda function that takes a key and checks if the value corresponding to the key in the dictionary is equal to K.
• Use the filter() function with the defined lambda function and the list of custom keys cust_keys to filter out the keys whose values are not equal to K.
• Convert the filtered result obtained in step 6 to a list and check if its length is equal to the length of the list cust_keys. If the lengths are equal, it means all the custom keys have values equal to K, so set res to True.
• Otherwise, set res to False.
• Print the result.

Python3

 `# Python3 code to demonstrate working of ` `# Test if custom keys equal to K in dictionary` `# Using filter() + lambda`   `# initializing dictionary` `test_dict ``=` `{``"Gfg"` `: ``5``, ``"is"` `: ``8``, ``"Best"` `: ``10``, ``"for"` `: ``8``, ``"Geeks"` `: ``8``}`   `# printing original dictionary` `print``(``"The original dictionary is : "` `+` `str``(test_dict))`   `# initializing custom keys list ` `cust_keys ``=` `[``"is"``, ``"for"``, ``"Geeks"``]`   `# initializing K ` `K ``=` `8`   `# using filter() and lambda function to check for all keys ` `res ``=` `all``(``filter``(``lambda` `x: test_dict[x] ``=``=` `K, cust_keys))`   `# printing result ` `print``(``"Are all custom keys equal to K : "` `+` `str``(res))`

Output

```The original dictionary is : {'Gfg': 5, 'is': 8, 'Best': 10, 'for': 8, 'Geeks': 8}
Are all custom keys equal to K : True```

Time Complexity:  O(m), where m is the number of keys in the test_dict dictionary.

Auxiliary Space: O(1), as no extra space is required.

Method #5 : Using count() and len() methods:

Approach:

1. Create an empty list x.
2. Initiate a for loop to append the values of custom keys to a list x.
3. Check whether the count of K in x is equal to the length of x(using count()). If yes, then print “Are all custom keys equal to K: True“, Otherwise, print “Are all custom keys equal to K: False“.

Below is the implementation of the above approach:

Python3

 `# Python3 code to demonstrate working of test` `# if custom keys equal to K in dictionary` `# Using loop`   `# initializing dictionary` `test_dict ``=` `{``"Gfg"``: ``5``, ``"is"``: ``8``, ``"Best"``: ``10``, ``"for"``: ``8``, ``"Geeks"``: ``8``}`   `# printing original dictionary` `print``(``"The original dictionary is : "` `+` `str``(test_dict))`   `# initializing custom keys list` `cust_keys ``=` `[``"is"``, ``"for"``, ``"Geeks"``]`   `# initializing K` `K ``=` `8`   `# using loop to check for all keys` `x ``=` `[]` `for` `i ``in` `cust_keys:` `    ``x.append(test_dict[i])` `res ``=` `x.count(K) ``=``=` `len``(x)`   `# printing result` `print``(``"Are all custom keys equal to K : "` `+` `str``(res))`

Output

```The original dictionary is : {'Gfg': 5, 'is': 8, 'Best': 10, 'for': 8, 'Geeks': 8}
Are all custom keys equal to K : True```

Time Complexity: O(N) N – length of cust_keys
Auxiliary Space: O(1)

Previous
Next