Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Find keys with duplicate values in dictionary in Python
While dealing with dictionaries, we may come across situations when there are duplicate values in the dictionary while the keys remain unique. In this article we will see how to find keys that share the same values using different approaches.
Method 1: Using Key-Value Exchange
We exchange the keys with values of the dictionaries and then keep appending the keys associated with a given value. This way the duplicate values get grouped together ?
Example
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}
print("Given Dictionary:", dictA)
k_v_exchanged = {}
for key, value in dictA.items():
if value not in k_v_exchanged:
k_v_exchanged[value] = [key]
else:
k_v_exchanged[value].append(key)
print("New Dictionary:", k_v_exchanged)
Given Dictionary: {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3}
New Dictionary: {5: ['Sun', 'Tue'], 3: ['Mon', 'Wed']}
Method 2: Using setdefault() with Sets
We follow a similar approach here but use the setdefault() method with sets. We create a new dictionary from the existing dictionary and add keys with duplicate values. Finally we filter out the values where the length is greater than 1 ?
Example
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 4}
print("Given Dictionary:", dictA)
dictB = {}
for key, value in dictA.items():
dictB.setdefault(value, set()).add(key)
res = filter(lambda x: len(x) > 1, dictB.values())
print("Keys with duplicate values:", list(res))
Given Dictionary: {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 4}
Keys with duplicate values: [{'Tue', 'Sun'}]
Method 3: Using Dictionary Comprehension
A more concise approach using dictionary comprehension to directly identify keys with duplicate values ?
Example
dictA = {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3, 'Thu': 7}
print("Given Dictionary:", dictA)
# Group keys by values
value_to_keys = {}
for key, value in dictA.items():
value_to_keys.setdefault(value, []).append(key)
# Find only duplicate values
duplicates = {value: keys for value, keys in value_to_keys.items() if len(keys) > 1}
print("Keys with duplicate values:", duplicates)
Given Dictionary: {'Sun': 5, 'Mon': 3, 'Tue': 5, 'Wed': 3, 'Thu': 7}
Keys with duplicate values: {5: ['Sun', 'Tue'], 3: ['Mon', 'Wed']}
Comparison
| Method | Output Format | Best For |
|---|---|---|
| Key-Value Exchange | Dictionary with all values | When you need all grouped keys |
| setdefault() with Sets | List of sets (duplicates only) | When you only want duplicates |
| Dictionary Comprehension | Clean dictionary format | Most readable and flexible |
Conclusion
Use dictionary comprehension for the cleanest approach to find keys with duplicate values. The key-value exchange method works well when you need to see all grouped keys, while setdefault() is useful for filtering only duplicates.
