How to Remove Duplicate Values from Dictionary in Python
To remove duplicate values from a
dictionary in Python, you can create a new dictionary by iterating over the original and keeping only the first occurrence of each value using a set to track seen values. This is commonly done with a dictionary comprehension combined with a set to filter duplicates.Syntax
You can remove duplicate values from a dictionary by creating a new dictionary using a dictionary comprehension. Use a set to keep track of values you have already seen and only add key-value pairs if the value is new.
General syntax:
seen = set()
new_dict = {k: v for k, v in original_dict.items() if v not in seen and (seen.add(v) or True)}Here, seen.add(v) or True adds the value to the set and returns True so the condition passes.
python
seen = set() new_dict = {k: v for k, v in original_dict.items() if v not in seen and (seen.add(v) or True)}
Example
This example shows how to remove duplicate values from a dictionary. The new dictionary keeps only the first key for each unique value.
python
original_dict = {'a': 1, 'b': 2, 'c': 1, 'd': 3, 'e': 2}
seen = set()
new_dict = {k: v for k, v in original_dict.items() if v not in seen and (seen.add(v) or True)}
print(new_dict)Output
{'a': 1, 'b': 2, 'd': 3}
Common Pitfalls
One common mistake is trying to remove duplicates by just converting dictionary values to a set, which loses the keys and order.
Another mistake is not preserving the first occurrence of each value, which can lead to unexpected results.
python
original_dict = {'a': 1, 'b': 2, 'c': 1}
# Wrong: loses keys and order
unique_values = set(original_dict.values())
print(unique_values)
# Right: preserves keys and order
seen = set()
new_dict = {k: v for k, v in original_dict.items() if v not in seen and (seen.add(v) or True)}
print(new_dict)Output
{1, 2}
{'a': 1, 'b': 2}
Quick Reference
- Use a
setto track seen values. - Use dictionary comprehension to build a new dictionary without duplicates.
- Preserve the first key for each unique value.
- Do not convert values directly to a set if you want to keep keys.
Key Takeaways
Use a set to track seen values when removing duplicates from a dictionary.
Dictionary comprehension helps create a new dictionary with unique values while preserving keys.
Preserve the first occurrence of each value to keep dictionary order and meaning.
Avoid converting dictionary values directly to a set if you want to keep keys.
This method works well for dictionaries with hashable values.