Given a dictionary, the task is to find keys with duplicate values. Let’s discuss a few methods for the same.
Method #1: Using Naive approach
In this method first, we convert dictionary values to keys with the inverse mapping and then find the duplicate keys
# Python code to demonstrate # finding duplicate values from a dictionary # initialising dictionary ini_dict = {'a':1, 'b':2, 'c':3, 'd':2} # printing initial_dictionary print("initial_dictionary", str(ini_dict)) # finding duplicate values # from dictionary # using a naive approach rev_dict = {} for key, value in ini_dict.items(): rev_dict.setdefault(value, set()).add(key) result = [key for key, values in rev_dict.items() if len(values) > 1] # printing result print("duplicate values", str(result)) |
initial_dictionary {'c': 3, 'b': 2, 'd': 2, 'a': 1}
duplicate values [2]
Method #2: Using flipping dictionary
# Python code to demonstrate # finding duplicate values from dictionary # initialising dictionary ini_dict = {'a':1, 'b':2, 'c':3, 'd':2} # printing initial_dictionary print("initial_dictionary", str(ini_dict)) # finding duplicate values # from dictionary using flip flipped = {} for key, value in ini_dict.items(): if value not in flipped: flipped[value] = [key] else: flipped[value].append(key) # printing result print("final_dictionary", str(flipped)) |
initial_dictionary {'a': 1, 'c': 3, 'd': 2, 'b': 2}
final_dictionary {1: ['a'], 2: ['d', 'b'], 3: ['c']}
Method #3: Using chain and set
Suppose you need to find keys having duplicate values.
# Python code to demonstrate # finding duplicate values from dictionary from itertools import chain # initialising dictionary ini_dict = {'a':1, 'b':2, 'c':3, 'd':2} # printing initial_dictionary print("initial_dictionary", str(ini_dict)) # finding duplicate values # from dictionary using set rev_dict = {} for key, value in ini_dict.items(): rev_dict.setdefault(value, set()).add(key) result = set(chain.from_iterable( values for key, values in rev_dict.items() if len(values) > 1)) # printing result print("resultant key", str(result)) |
initial_dictionary {'b': 2, 'd': 2, 'c': 3, 'a': 1}
resultant key {'d', 'b'}
Instead of
result = set(chain.from_iterable( values for key, values in rev_dict.items() if len(values) > 1)) |
Use this:
result = filter(lambda x: len(x)>1, rev_dict.values()) print(list(result)) |
Recommended Posts:
- Python - Append Dictionary Keys and Values ( In order ) in dictionary
- Python - Remove duplicate values across Dictionary Values
- Python - Extract selective keys' values Including Nested Keys
- Python | Split dictionary keys and values into separate lists
- Python program to Swap Keys and Values in Dictionary
- Python | Combine two dictionary adding values for common keys
- Python | Test if dictionary contains unique keys and values
- Python - Assign values to initialized dictionary keys
- Python - Unique value keys in a dictionary with lists as values
- Python - Keys associated with Values in Dictionary
- Python - Sort Dictionary by Values and Keys
- Python - Dictionary Keys whose Values summation equals K
- Python - Remove duplicate values in dictionary
- Python - Remove keys with Values Greater than K ( Including mixed values )
- Python - Filter dictionary values in heterogenous dictionary
- Python - Extract Unique values dictionary values
- Python - Test for Even values dictionary values lists
- Python - Find Keys with specific suffix in Dictionary
- Python dictionary with keys having multiple inputs
- Python Dictionary | keys() method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : AyeshaBhatnagar

