Wednesday, December 25, 2024
Google search engine
HomeLanguagesPython – Remove duplicate values in dictionary

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. 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, 'Lazyroar': 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, ‘Lazyroar’: 20, ‘is’: 15, ‘best’: 20} The dictionary after values removal : {‘gfg’: 10, ‘Lazyroar’: 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, 'Lazyroar': 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, ‘Lazyroar’: 20, ‘is’: 15, ‘best’: 20} The dictionary after values removal : {‘gfg’: 10, ‘Lazyroar’: 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:

  1. Initialize an empty dictionary.
  2. Loop through each key-value pair in the original dictionary.
  3. Use the setdefault() method to add the key-value pair to the new dictionary if the value is not already present in the dictionary.
  4. Return the new dictionary.

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, 'Lazyroar': 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())))


Output

The original dictionary is : {'gfg': 10, 'is': 15, 'best': 20, 'for': 10, 'Lazyroar': 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:

  1. Initialize an empty dictionary res.
  2. Loop through the values of the original dictionary test_dict using the values() method.
  3. Check if the value is already in the res dictionary using the if not val in res.values() condition.
  4. If the value is not already in the res dictionary, add it as a key to the res dictionary with a default value of None.
  5. Return the res dictionary.

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, 'Lazyroar': 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))


Output

The original dictionary is : {'gfg': 10, 'is': 15, 'best': 20, 'for': 10, 'Lazyroar': 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:

  1. Import the defaultdict module from the collections library.
  2. Initialize a defaultdict object and set its default value to an empty list.
  3. Iterate through the items of the dictionary and append the key to the list of the value in the defaultdict object.
  4. Create a new dictionary by iterating through the items of the defaultdict object and selecting the first value of each list.
  5. Print the resulting dictionary.

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, 'Lazyroar': 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))


Output

The original dictionary is : {'gfg': 10, 'is': 15, 'best': 20, 'for': 10, 'Lazyroar': 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:

  • Create an empty list called seen_values to keep track of the values that have already been seen.
  • Create a new dictionary called result_dict to store the non-duplicate values.
  • Loop through the key-value pairs in the original dictionary (test_dict).
  • For each key-value pair, check if the value is already in seen_values.
  • If the value is not in seen_values, add the key-value pair to result_dict.
  • If the value is already in seen_values, skip it.
  • Add the value to seen_values.
  • Return the result_dict.

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, 'Lazyroar' : 20}
result_dict = remove_duplicate_values_dict(test_dict)
print(result_dict)


Output

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, 'Lazyroar' : 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))


Output

The original dictionary is : {'gfg': 10, 'is': 15, 'best': 20, 'for': 10, 'Lazyroar': 20}
The dictionary after values removal : {'gfg': 10, 'is': 15, 'best': 20}

Time Complexity: O(n)
Auxiliary Space: O(n)

RELATED ARTICLES

Most Popular

Recent Comments