Python | Find keys with duplicate values in dictionary
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 program to Swap Keys and Values in Dictionary
- Python | Test if dictionary contains unique keys and values
- Python | Split dictionary keys and values into separate lists
- Python | Combine two dictionary adding values for common keys
- Python program to find the highest 3 values in a dictionary
- Python | Add new keys to a dictionary
- Python | Get total keys in dictionary
- Python | Get dictionary keys as a list
- Python | Minimum value keys in Dictionary
- Python | Add keys to nested dictionary
- Python | Count keys with particular value in dictionary
- Python Dictionary | keys() method
- Python | Grouping dictionary keys by value
- Python | Remove duplicate dictionaries from nested dictionary
- Python | Remove Keys from dictionary starting with K
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



