Remove duplicate values from dictionary Dry Run in PYTHON

Remove duplicate values from dictionary is an interactive PYTHON dry run visualizer from the Dict programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.

This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.

Remove duplicate values from dictionary Program Code

# Remove duplicate values from dictionary (keep first occurrence)
data = {"P1": 100, "P2": 200, "P3": 100, "P4": 300, "P5": 200, "P6": 400}
print("Original:", data)

# Method 1: Keep first occurrence of each value
seen_values = set()
unique_dict = {}
for key, value in data.items():
    if value not in seen_values:
        seen_values.add(value)
        unique_dict[key] = value
print("After removing duplicate values (keep first):", unique_dict)

# Method 2: Keep last occurrence
reverse_dict = {}
for key, value in data.items():
    reverse_dict[value] = key
# Then swap back (loses keys, keeps last occurrence)
result = {v: k for k, v in reverse_dict.items()}
print("Keep last occurrence:", result)

# Method 3: Get values that appear only once
from collections import Counter
value_counts = Counter(data.values())
unique_values = {k: v for k, v in data.items() if value_counts[v] == 1}
print("Values that appear exactly once:", unique_values)

View the complete Dict programs page.

Program Console Remove duplicate values from dictionary Topic: PYTHON Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.