Last Updated : 27 Apr, 2023
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.
Python3
# Python3 code to demonstrate working of
# Remove duplicate values in dictionary
# Using loop
# initializing dictionary
test_dict = {'gfg': 10, 'is': 15, 'best': 20, 'for': 10, 'geeks': 20}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Remove duplicate values in dictionary
# Using loop
temp = []
res = dict()
for key, val in test_dict.items():
if val not in temp:
temp.append(val)
res[key] = val
# printing result
print("The dictionary after values removal : " + str(res))
Output :
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.
Python3
# Python3 code to demonstrate working of
# Remove duplicate values in dictionary
# Using dictionary comprehension
# initializing dictionary
test_dict = {'gfg': 10, 'is': 15, 'best': 20, 'for': 10, 'geeks': 20}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Remove duplicate values in dictionary
# Using dictionary comprehension
temp = {val: key for key, val in test_dict.items()}
res = {val: key for key, val in temp.items()}
# printing result
print("The dictionary after values removal : " + str(res))
Output :
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:
Python3
# Python3 code to demonstrate working of
# Remove duplicate values in dictionary
# Using setdefault() method
# initializing dictionary
test_dict = {'gfg': 10, 'is': 15, 'best': 20, 'for': 10, 'geeks': 20}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Remove duplicate values in dictionary
# Using setdefault() method
res = {}
for key, val in test_dict.items():
res.setdefault(val, key)
# printing result
print("The dictionary after values removal : " +
str(dict((v, k) for k, v in res.items())))
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:
Python3
# Python3 code to demonstrate working of
# Remove duplicate values in dictionary
# Using values() method and set()
# initializing dictionary
test_dict = {'gfg': 10, 'is': 15, 'best': 20, 'for': 10, 'geeks': 20}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Remove duplicate values in dictionary
# Using values() method and set()
unique_values = set(test_dict.values())
res = {}
for val in unique_values:
for key in test_dict.keys():
if test_dict[key] == val:
res[key] = val
break
# Printing result
print("The dictionary after values removal : " + str(res))
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:
Python3
from collections import defaultdict
# initializing dictionary
test_dict = {'gfg': 10, 'is': 15, 'best': 20, 'for': 10, 'geeks': 20}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Remove duplicate values in dictionary
# Using defaultdict
d = defaultdict(list)
for k, v in test_dict.items():
d[v].append(k)
res = {k: v[0] for k, v in d.items()}
# printing result
print("The dictionary after values removal : " + str(res))
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:
Python3
def remove_duplicate_values_dict(test_dict):
seen_values = []
result_dict = {}
for key, value in test_dict.items():
if value not in seen_values:
result_dict[key] = value
seen_values.append(value)
print(f"Seen values so far: {seen_values}")
return result_dict
test_dict = { 'gfg' : 10, 'is' : 15, 'best' : 20, 'for' : 10, 'geeks' : 20}
result_dict = remove_duplicate_values_dict(test_dict)
print(result_dict)
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.
Python3
# Python3 code to demonstrate working of
# Remove duplicate values in dictionary
# Using dict.fromkeys() and keys()
# initializing dictionary
test_dict = { 'gfg' : 10, 'is' : 15, 'best' : 20, 'for' : 10, 'geeks' : 20}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Remove duplicate values in dictionary
# Using dict.fromkeys() and keys()
vals_dict = dict.fromkeys(test_dict.values(), None)
res = {}
for key, val in test_dict.items():
if vals_dict[val] is None:
vals_dict[val] = key
res[key] = val
# printing result
print("The dictionary after values removal : " + str(res))
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)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4