VOOZH about

URL: https://www.geeksforgeeks.org/python/python-program-to-remove-duplicity-from-a-dictionary/

⇱ Remove Duplicity from a Dictionary - Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Remove Duplicity from a Dictionary - Python

Last Updated : 23 Jul, 2025

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.


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.


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.


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.
Comment