Remove Duplicity from a Dictionary - Python Last Updated : 20 Feb, 2025 Summarize Comments Improve Suggest changes Share 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 loopThis 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 valuesHere are some more methods for removing duplicity:Table of ContentUsing a dictionary comprehensionUsing set()Using dict.fromkeys()Using a dictionary comprehensionThis 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. Comment More infoAdvertise with us Next Article Remove Duplicity from a Dictionary - Python M manjeet_04 Follow Improve Article Tags : Python Python Programs Python dictionary-programs python Practice Tags : pythonpython Similar Reads Python Remove Dictionary Item Sometimes, we may need to remove a specific item from a dictionary to update its structure. For example, consider the dictionary d = {'x': 100, 'y': 200, 'z': 300}. If we want to remove the item associated with the key 'y', several methods can help achieve this. Letâs explore these methods.Using pop 2 min read Python - Remove duplicate values in dictionary Sometimes, while working with Python dictionaries, we can have problem in which we need to perform the removal of all the duplicate values of dictionary, and we are not concerned if any key get removed in the process. This kind of application can occur in school programming and day-day programming. 8 min read Remove Duplicate Dictionaries from Nested Dictionary - Python We are given a nested dictionary we need to remove the duplicate dictionaries from the nested dictionary. For example we are given a nested dictionary d = {'key1': [{'a': 1}, {'b': 2}, {'a': 1}], 'key2': [{'x': 3}, {'y': 4}]} we need to remove the duplicate dictionary from this dictionary so output 4 min read Python - Remove Item from Dictionary There are situations where we might want to remove a specific key-value pair from a dictionary. For example, consider the dictionary d = {'x': 10, 'y': 20, 'z': 30}. If we need to remove the key 'y', there are multiple ways to achieve this. Let's discuss several methods to remove an item from a dict 3 min read Python - Remove Disjoint Tuple Keys from Dictionary We are given a dictionary we need to remove the Disjoint Tuple key from it. For example we are given a dictionary d = {('a', 'b'): 1, ('c',): 2, ('d', 'e'): 3, 'f': 4} we need to remove all the disjoint tuple so that the output should be { }. We can use multiple methods like dictionary comprehension 3 min read Like