October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Find Duplicate Values in a Python Dictionary

Find repeated dictionary values with Counter, group matching keys, or use a one-pass set when you only need duplicate detection.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use collections.Counter to find repeated hashable values, or group dictionary keys by value when you need to know which keys match. For lists or dictionaries stored as values, use an equality-based approach rather than passing them directly to Counter or using them as dictionary keys.

Find which values appear more than once

Count the dictionary’s values, then keep only values with a count greater than one:

from collections import Counter

d = {"a": 1, "b": 2, "c": 1, "d": 3, "e": 2}
counts = Counter(d.values())
duplicate_values = [value for value, count in counts.items() if count > 1]

print(duplicate_values)  # [1, 2]

Counter is part of Python’s standard library. This produces each repeated value once; use counts itself if you also need the number of occurrences.

Find which keys share each value

To identify the original keys associated with each repeated value, build a reverse mapping from values to lists of keys, then discard groups containing only one key:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
from collections import defaultdict

groups = defaultdict(list)
for key, value in d.items():
    groups[value].append(key)

duplicate_groups = {
    value: keys for value, keys in groups.items() if len(keys) > 1
}

print(duplicate_groups)  # {1: ['a', 'c'], 2: ['b', 'e']}

You can use dict.setdefault instead of defaultdict if you prefer not to import it:

groups = {}
for key, value in d.items():
    groups.setdefault(value, []).append(key)

Filter groups in the same way to keep only lists with more than one key.

Choose an approach for the result you need

Goal Approach Requirement
Unique repeated values Count with Counter and filter counts greater than one Values must be hashable
Occurrence counts Use Counter(d.values()) Values must be hashable
Keys grouped by repeated value Collect keys into lists keyed by value Values used as grouping keys must be hashable
Boolean or unique repeated values in one pass Track values in seen and duplicates sets Values must be hashable

A one-pass variant is useful when you do not need a full count table:

seen = set()
duplicates = set()

for value in d.values():
    if value in seen:
        duplicates.add(value)
    else:
        seen.add(value)

has_duplicates = bool(duplicates)

duplicates contains each repeated value once. Sets do not preserve a sorted or input-independent order, so sort the results explicitly if a particular display order matters and the values can be ordered.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What if the values are lists or dictionaries?

Lists and dictionaries are unhashable, so they cannot be used directly as keys in Counter, a set, or the reverse-mapping approach above. If your values can have these types, compare them using the equality rules appropriate to your data, or normalize them into a stable hashable form. Do not convert arbitrary structures to strings as a shortcut: string representations are not a general definition of data equality.

For nested or custom objects, decide what should count as equal before choosing a comparison or normalization strategy. There is no single representation that is correct for every data shape.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Why duplicate values are allowed

Dictionary keys are unique within a dictionary, but values do not have to be. Python’s PEP 3106 explains: “The object returned by the values() method behaves like a much simpler unordered collection – it cannot be a set because duplicate values are possible.” PEP 3106

Modern Python dictionaries preserve insertion order as a language guarantee starting with Python 3.7. Iterating through d.items() therefore visits entries in that order, and replacing a value for an existing key does not change that key’s position. The duplicate values themselves are still not a set-like, unique collection.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.