Open In App

Remove Duplicity from a Dictionary - Python

Last Updated : 20 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We are given a dictionary and our task is to remove duplicate values from it. For example, if the dictionary is {'a': 1, 'b': 2, 'c': 2, 'd': 3, 'e': 1}, the unique values are {1, 2, 3}, so the output should be {'a': 1, 'b': 2, 'd': 3}.

Using a loop

This method uses a loop to iterate through dictionary and checks if a value has already been added to new dictionary, if not then it adds key-value pair.

Python
d = {'a': 1, 'b': 2, 'c': 2, 'd': 3, 'e': 1}
u = {}

for key, value in d.items():
    # Check if  value is not  in the dictionary 
    if value not in u.values():
        # Add the key-value pair to dictionary
        u[key] = value

print(u)

Output
{'a': 1, 'b': 2, 'd': 3}

Explanation:

  • Code iterates over original dictionary "d" and checks whether each value is already present in new dictionary "u" to ensure there are no duplicates.
  • If a value is not already in "u" corresponding key-value pair is added to "u" resulting in a dictionary with unique values

Here are some more methods for removing duplicity:

Using a dictionary comprehension

This approach uses a dictionary comprehension with a condition to ensure that only unique values are included.

Python
d = {'a': 1, 'b': 2, 'c': 2, 'd': 3, 'e': 1}
s = set()

# Dictionary comprehension to remove duplicate
u = {key: value for key, value in d.items() if value not in s and not s.add(value)}

print(u)

Output
{'a': 1, 'b': 2, 'd': 3}

Explanation:

  • "s" is a set that tracks seen values to ensure uniqueness.
  • if value not in s and not s.add(value) keeps only the first occurrence of each value.
  • final dictionary contains only unique values.

Using dict.fromkeys()

This approach uses dict.fromkeys() with a set to remove duplicates and then uses a dictionary comprehension to reassign key-value pairs.

Python
d = {'a': 1, 'b': 2, 'c': 2, 'd': 3, 'e': 1}
u = dict.fromkeys(d.values())

# Dictionary comprehension 
temp = dict.fromkeys(d.values())  # Track first occurrences
u = {}
for key, value in d.items():
    if value in temp:
        u[key] = value
        del temp[value]  # Remove value after first use

print(u)

Output
{'a': 1, 'b': 2, 'd': 3}

Explanation:

  • We use dict.fromkeys(d.values()) to create a temporary dictionary u where keys are unique values from original dictionary "d" effectively eliminating duplicates.
  • It then creates a new dictionary by iterating over d.items() and keeping only key-value pairs where value is in temporary dictionary u ensuring that only first occurrence of each value is retained.

Next Article

Similar Reads