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
test_dict = { 5 : 5 , 8 : 9 , 7 : 7 , 1 : 2 , 10 : 10 , 4 : 8 }
print ( "The original dictionary is : " + str (test_dict))
res = 0
for key in test_dict:
if key = = test_dict[key]:
res + = 1
print ( "The required frequency : " + str (res))
|
OutputThe 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
test_dict = { 5 : 5 , 8 : 9 , 7 : 7 , 1 : 2 , 10 : 10 , 4 : 8 }
print ( "The original dictionary is : " + str (test_dict))
res = sum ([ 1 for key in test_dict if key = = test_dict[key]])
print ( "The required frequency : " + str (res))
|
OutputThe 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
test_dict = { 5 : 5 , 8 : 9 , 7 : 7 , 1 : 2 , 10 : 10 , 4 : 8 }
print ( "The original dictionary is : " + str (test_dict))
filtered_list = list ( filter ( lambda x: x[ 0 ] = = x[ 1 ], test_dict.items()))
res = len (filtered_list)
print ( "The required frequency : " + str (res))
|
OutputThe 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
test_dict = { 5 : 5 , 8 : 9 , 7 : 7 , 1 : 2 , 10 : 10 , 4 : 8 }
print ( "The original dictionary is : " + str (test_dict))
res = len ({k: v for k, v in test_dict.items() if k = = v})
print ( "The required frequency : " + str (res))
|
OutputThe 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).