Python - Keys Values equal frequency
Last Updated :
18 May, 2023
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))
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
# 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))
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
# 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))
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
# 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))
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).
Similar Reads
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 - 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 | Elements with Frequency equal K This is one of the most essential operation that programmer quite often comes in terms with. Be it development or competitive programming, this utility is quite essential to master as it helps to perform many tasks that involve this task to be its subtask. Lets discuss approach to achieve this opera
3 min read
Python - Elements frequency in Tuple Given a Tuple, find the frequency of each element. Input : test_tup = (4, 5, 4, 5, 6, 6, 5) Output : {4: 2, 5: 3, 6: 2} Explanation : Frequency of 4 is 2 and so on.. Input : test_tup = (4, 5, 4, 5, 6, 6, 6) Output : {4: 2, 5: 2, 6: 3} Explanation : Frequency of 4 is 2 and so on.. Method #1 Using def
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 - Values frequency across Dictionaries lists Given two list of dictionaries, compute frequency corresponding to each value in dictionary 1 to second. Input : test_list1 = [{"Gfg" : 6}, {"best" : 10}], test_list2 = [{"a" : 6}, {"b" : 10}, {"d" : 6}}] Output : {'Gfg': 2, 'best': 1} Explanation : 6 has 2 occurrence in 2nd list, 10 has 1. Input :
6 min read