![]() |
VOOZH | about |
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. Let's discuss certain ways in which this task can be performed.
Method #1 : Using loop This is the brute force way in which we perform this task. In this, we keep track of occurred value, and remove it if it repeats.
The original dictionary is : {'gfg': 10, 'for': 10, 'geeks': 20, 'is': 15, 'best': 20} The dictionary after values removal : {'gfg': 10, 'geeks': 20, 'is': 15}
Time complexity: O(n), where n is the number of elements in the dictionary.
Auxiliary space: O(n), as we are using a temporary list of size n to store unique values.
Method #2: Using dictionary comprehension
The following problem can also be performed using dictionary comprehension. In this, we perform task in similar way as above method, just as a shorthand.
The original dictionary is : {'gfg': 10, 'for': 10, 'geeks': 20, 'is': 15, 'best': 20} The dictionary after values removal : {'gfg': 10, 'geeks': 20, 'is': 15}
Time complexity: O(n), where n is the number of elements in the dictionary.
Auxiliary space: O(n), where n is the number of elements in the dictionary.
Method 3: use the setdefault() method.
Follow the below steps:
Below is the implementation of the above approach:
The original dictionary is : {'gfg': 10, 'is': 15, 'best': 20, 'for': 10, 'geeks': 20}
The dictionary after values removal : {'gfg': 10, 'is': 15, 'best': 20}Time complexity: O(n)
Auxiliary space: O(n)
Method #4: Using the values() method and set()
Approach:
Below is the implementation of the above approach:
The original dictionary is : {'gfg': 10, 'is': 15, 'best': 20, 'for': 10, 'geeks': 20}
The dictionary after values removal : {'gfg': 10, 'best': 20, 'is': 15}Time complexity: O(n^2), where n is the number of key-value pairs in the original dictionary.
Auxiliary space: O(n), where n is the number of unique values in the original dictionary.
Method #5: Using collections.defaultdict
Approach:
Below is the implementation of the above approach:
The original dictionary is : {'gfg': 10, 'is': 15, 'best': 20, 'for': 10, 'geeks': 20}
The dictionary after values removal : {10: 'gfg', 15: 'is', 20: 'best'}Time complexity: O(n), where n is the number of items in the dictionary.
Auxiliary space: O(n), for the defaultdict object.
Method #6: Using a list comprehension
Step-by-step approach:
Below is the implementation of the above approach:
Seen values so far: [10]
Seen values so far: [10, 15]
Seen values so far: [10, 15, 20]
Seen values so far: [10, 15, 20]
Seen values so far: [10, 15, 20]
{'gfg': 10, 'is': 15, 'best': 20}Time complexity: O(n), where n is the number of key-value pairs in the input dictionary.
Auxiliary space: O(n), where n is the number of unique values in the input dictionary.
Method #7: Using dict.fromkeys() and keys()
The dict.fromkeys() method can be used to create a new dictionary with keys from an iterable and values set to a default value. In this case, we can create a dictionary with keys from the values of the original dictionary and values set to None. We can then use a loop to iterate over the original dictionary and set the values of the new dictionary to the corresponding key if the value has not already been encountered.
The original dictionary is : {'gfg': 10, 'is': 15, 'best': 20, 'for': 10, 'geeks': 20}
The dictionary after values removal : {'gfg': 10, 'is': 15, 'best': 20}Time Complexity: O(n)
Auxiliary Space: O(n)