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 value.
Method #1: Using loop
In this, we count instances where keys are equal to values and increment the counter accordingly.
Python3
# Python3 code to demonstrate working of # Keys Values equal frequency # Using loop # initializing dictionary test_dict = { 5 : 5 , 8 : 9 , 7 : 7 , 1 : 2 , 10 : 10 , 4 : 8 } # printing original dictionary print ( "The original dictionary is : " + str (test_dict)) res = 0 for key in test_dict: # checking for equality and incrementing counter if key = = test_dict[key]: res + = 1 # printing result print ( "The required frequency : " + str (res)) |
The original dictionary is : {5: 5, 8: 9, 7: 7, 1: 2, 10: 10, 4: 8} The required frequency : 3
Time complexity: O(n), where n is the number of values in the dictionary.
Auxiliary Space: O(1), constant extra space is required
Method #2 : Using sum() + list comprehension
In this, the task of counting is performed using sum(), when equal key values are found, 1 is appended to the list, and then at the end, each value is summed.
Python3
# Python3 code to demonstrate working of # Keys Values equal frequency # Using sum() + list comprehension # Initializing dictionary test_dict = { 5 : 5 , 8 : 9 , 7 : 7 , 1 : 2 , 10 : 10 , 4 : 8 } # Printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # Computing summation to get frequency res = sum ([ 1 for key in test_dict if key = = test_dict[key]]) # Printing result print ( "The required frequency : " + str (res)) |
The original dictionary is : {5: 5, 8: 9, 7: 7, 1: 2, 10: 10, 4: 8} The required frequency : 3
Method #3: Using filter() + lambda function
Approach:
- Initialize the dictionary.
- Use filter() function along with lambda function to filter out the key-value pairs where key and value are equal.
- Find the length of the filtered list to get the required frequency.
- Print the result.
Below is the implementation of the above approach:
Python3
# Python3 code to demonstrate working of # Keys Values equal frequency # using filter() + lambda # Initializing dictionary test_dict = { 5 : 5 , 8 : 9 , 7 : 7 , 1 : 2 , 10 : 10 , 4 : 8 } # Printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # Getting key-value pairs where key and value are equal # using filter() and lambda filtered_list = list ( filter ( lambda x: x[ 0 ] = = x[ 1 ], test_dict.items())) # Getting the length of filtered list to get the required frequency res = len (filtered_list) # Printing result print ( "The required frequency : " + str (res)) |
The original dictionary is : {5: 5, 8: 9, 7: 7, 1: 2, 10: 10, 4: 8} The required frequency : 3
Time complexity: O(n), where n is the number of items in the dictionary.
Auxiliary space: O(k), where k is the number of key-value pairs where key and value are equal.
Method #4: Using dictionary comprehension
Stepwise approach:
- Initialize the dictionary test_dict.
- Print the original dictionary.
- Use dictionary comprehension to iterate over the items of test_dict and filter the keys that have the same value as their key. Here, we create a new dictionary with the same keys and values as test_dict but only keep the items where the key is equal to the value.
- Take the length of the resulting dictionary to get the frequency of keys with the same value as key.
- Print the result.
Python3
# Python3 code to demonstrate working of # Keys Values equal frequency # using dictionary comprehension # Initializing dictionary test_dict = { 5 : 5 , 8 : 9 , 7 : 7 , 1 : 2 , 10 : 10 , 4 : 8 } # Printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # Filter keys with same value as key # using dictionary comprehension res = len ({k: v for k, v in test_dict.items() if k = = v}) # Printing result print ( "The required frequency : " + str (res)) |
The original dictionary is : {5: 5, 8: 9, 7: 7, 1: 2, 10: 10, 4: 8} The required frequency : 3
Time complexity: O(n), where n is the number of items in the dictionary.
Auxiliary space: O(n), where n is the number of items in the dictionary (to store the filtered dictionary).