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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:

initial_dictionary {'c': 3, 'b': 2, 'd': 2, 'a': 1}
duplicate values [2]

 
Method #2: Using flipping dictionary

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:

initial_dictionary {'b': 2, 'd': 2, 'c': 3, 'a': 1}
resultant key {'d', 'b'}


My Personal Notes arrow_drop_up

Programming freaktech Enthusiast and have interest in learning new upcoming technologies

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.