Open In App

Python – Remove duplicate values across Dictionary Values

Last Updated : 11 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python dictionaries, we can have a problem in which we need to remove all the duplicate values across all the dictionary value lists. This problem can have applications in data domains and web development domains. Let’s discuss certain ways in which this task can be performed.

Input: test_dict = {‘Manjeet’: [1], ‘Akash’: [1, 8, 9]} 
Output: {‘Manjeet’: [], ‘Akash’: [8, 9]} 

Input: test_dict = {‘Manjeet’: [1, 1, 1], ‘Akash’: [1, 1, 1]} 
Output: {‘Manjeet’: [], ‘Akash’: []}

Method #1: Using Counter() + list comprehension 

The combination of the above functions can be used to solve this problem. In this, we use Counter() to extract all frequencies and list comprehension to assign the value with a single occurrence in the value list. 

Python3




# Python3 code to demonstrate working of
# Remove duplicate values across Dictionary Values
# Using Counter() + list comprehension
from collections import Counter
 
# initializing dictionary
test_dict = {'Manjeet': [1, 4, 5, 6],
             'Akash': [1, 8, 9],
             'Nikhil': [10, 22, 4],
             'Akshat': [5, 11, 22]}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Remove duplicate values across Dictionary Values
# Using Counter() + list comprehension
cnt = Counter()
for idx in test_dict.values():
    cnt.update(idx)
res = {idx: [key for key in j if cnt[key] == 1]
       for idx, j in test_dict.items()}
 
# printing result
print("Uncommon elements records : " + str(res))


Output

The original dictionary : {'Manjeet': [1, 4, 5, 6], 'Akash': [1, 8, 9], 'Nikhil': [10, 22, 4], 'Akshat': [5, 11, 22]}
Uncommon elements records : {'Manjeet': [6], 'Akash': [8, 9], 'Nikhil': [10], 'Akshat': [11]}

Time complexity: O(n*m), where n is number of keys in dictionary and m is length of the longest list in dictionary.
Auxiliary space: O(n*m), where n is number of keys in dictionary and m is length of the longest list in dictionary.

Method #2 : Using extend(),count(),keys() methods and for loops

Python3




# Python3 code to demonstrate working of
# Remove duplicate values across Dictionary Values
 
# initializing dictionary
test_dict = {'Manjeet': [1, 4, 5, 6],
             'Akash': [1, 8, 9],
             'Nikhil': [10, 22, 4],
             'Akshat': [5, 11, 22]}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Remove duplicate values across Dictionary Values
x = []
for i in test_dict.keys():
    x.extend(test_dict[i])
y = []
for i in x:
    if(x.count(i) == 1):
        y.append(i)
res = dict()
 
for i in test_dict.keys():
    a = []
    for j in test_dict[i]:
        if j in y:
            a.append(j)
        res[i] = a
 
# printing result
print("Uncommon elements records : " + str(res))


Output

The original dictionary : {'Manjeet': [1, 4, 5, 6], 'Akash': [1, 8, 9], 'Nikhil': [10, 22, 4], 'Akshat': [5, 11, 22]}
Uncommon elements records : {'Manjeet': [6], 'Akash': [8, 9], 'Nikhil': [10], 'Akshat': [11]}

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

Method #3: Using extend(),operator.countOf(),keys() methods and for loops

Python3




# Python3 code to demonstrate working of
# Remove duplicate values across Dictionary Values
import operator as op
# initializing dictionary
test_dict = {'Manjeet': [1, 4, 5, 6],
             'Akash': [1, 8, 9],
             'Nikhil': [10, 22, 4],
             'Akshat': [5, 11, 22]}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Remove duplicate values across Dictionary Values
x = []
for i in test_dict.keys():
    x.extend(test_dict[i])
y = []
for i in x:
    if(op.countOf(x,i) == 1):
        y.append(i)
res = dict()
 
for i in test_dict.keys():
    a = []
    for j in test_dict[i]:
        if j in y:
            a.append(j)
        res[i] = a
 
# printing result
print("Uncommon elements records : " + str(res))


Output

The original dictionary : {'Manjeet': [1, 4, 5, 6], 'Akash': [1, 8, 9], 'Nikhil': [10, 22, 4], 'Akshat': [5, 11, 22]}
Uncommon elements records : {'Manjeet': [6], 'Akash': [8, 9], 'Nikhil': [10], 'Akshat': [11]}

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

Method #4: Using defaultdict() and set()

Step-by-step approach:

  • Import defaultdict from collections
  • Initialize a defaultdict with set as its default_factory
  • Loop through the values of the test_dict
    • Loop through the items of each value
      • Add each item to the corresponding set in the defaultdict
  • Loop through the items of the test_dict
    • Initialize a new list for each item
    • Loop through the items of the value and check if the length of the corresponding set in the defaultdict is 1
      • If the length is 1, append the item to the new list
      • Add the new list to the result dictionary using the item as the key
  • Print the result dictionary

Below is the implementation of the above approach:

Python3




from collections import defaultdict
 
test_dict = {'Manjeet': [1, 4, 5, 6],
             'Akash': [1, 8, 9],
             'Nikhil': [10, 22, 4],
             'Akshat': [5, 11, 22]}
 
print("The original dictionary : " + str(test_dict))
 
d = defaultdict(set)
 
for lst in test_dict.values():
    for item in lst:
        d[item].add(id(lst))
 
res = {}
for k, lst in test_dict.items():
    new_lst = []
    for item in lst:
        if len(d[item]) == 1:
            new_lst.append(item)
    res[k] = new_lst
 
print("Uncommon elements records : " + str(res))


Output

The original dictionary : {'Manjeet': [1, 4, 5, 6], 'Akash': [1, 8, 9], 'Nikhil': [10, 22, 4], 'Akshat': [5, 11, 22]}
Uncommon elements records : {'Manjeet': [6], 'Akash': [8, 9], 'Nikhil': [10], 'Akshat': [11]}

Time complexity: O(n*m), where n is the number of keys in the dictionary and m is the length of the longest list in the dictionary.
Auxiliary space: O(n*m), for the defaultdict and the result dictionary.

Method #5: Using list comprehension and dictionary comprehension

Step-by-step approach:

  • Create a flattened list of all values in the dictionary using list comprehension and the values() method of the dictionary.
  • Create a new list unique_values containing only the values that occur exactly once in the flattened list using list comprehension and the count() method.
  • Create a new dictionary res using dictionary comprehension where each key is a key from the original dictionary and each value is a list of values from the original dictionary that occur in unique_values.

Below is the implementation of the above approach:

Python3




# Python3 code to demonstrate working of
# Remove duplicate values across Dictionary Values
from collections import Counter
 
# initializing dictionary
test_dict = {'Manjeet': [1, 4, 5, 6],
             'Akash': [1, 8, 9],
             'Nikhil': [10, 22, 4],
             'Akshat': [5, 11, 22]}
 
# printing original dictionary
print("The original dictionary : " + str(test_dict))
 
# Remove duplicate values across Dictionary Values
flat_list = [val for sublist in test_dict.values() for val in sublist]
unique_values = [val for val, count in Counter(flat_list).items() if count == 1]
res = {key: [val for val in values if val in unique_values] for key, values in test_dict.items()}
 
# printing result
print("Uncommon elements records : " + str(res))


Output

The original dictionary : {'Manjeet': [1, 4, 5, 6], 'Akash': [1, 8, 9], 'Nikhil': [10, 22, 4], 'Akshat': [5, 11, 22]}
Uncommon elements records : {'Manjeet': [6], 'Akash': [8, 9], 'Nikhil': [10], 'Akshat': [11]}

Time complexity: O(n + m + k), where n is the total number of values in the original dictionary, m is the number of unique values in the original dictionary, and k is the number of keys in the original dictionary.
Auxiliary space: O(n + m + k), where n is the total number of values in the original dictionary, m is the number of unique values in the original dictionary, and k is the number of keys in the original dictionary. 

Method #6:  Using numpy:

Algorithm:

  1. Create an empty dictionary ‘d’ with default values as a set.
  2. Iterate through each value list in the input dictionary and add the id of the list to the set corresponding to each element in the list in the dictionary ‘d’.
  3. Create a new empty dictionary ‘res’.
  4. Iterate through each key-value pair in the input dictionary and for each list in the value, create a new list ‘new_lst’.
  5. For each item in the list, check if its corresponding set in ‘d’ has length 1, indicating it is unique.
  6. If it is unique, append it to the ‘new_lst’.
  7. Add the ‘new_lst’ as the value for the current key in the ‘res’ dictionary.
  8. Print the ‘res’ dictionary as the output.

Python3




import numpy as np
 
test_dict = {'Manjeet': [1, 4, 5, 6],
            'Akash': [1, 8, 9],
            'Nikhil': [10, 22, 4],
            'Akshat': [5, 11, 22]}
 
print("The original dictionary : " + str(test_dict))
 
d = {}
for lst in test_dict.values():
    unique_lst = np.unique(lst)
    for item in unique_lst:
        if item in d:
            d[item] += 1
        else:
            d[item] = 1
 
res = {}
for k, lst in test_dict.items():
    new_lst = []
    for item in lst:
        if d[item] == 1:
            new_lst.append(item)
    res[k] = new_lst
 
print("Uncommon elements records : " + str(res))
#This code is contributed by Jyothi pinjala.


Output:

The original dictionary : {‘Manjeet’: [1, 4, 5, 6], ‘Akash’: [1, 8, 9], ‘Nikhil’: [10, 22, 4], ‘Akshat’: [5, 11, 22]}
Uncommon elements records : {‘Manjeet’: [6], ‘Akash’: [8, 9], ‘Nikhil’: [10], ‘Akshat’: [11]}

Time complexity:
The time complexity of this code is O(n * m), where ‘n’ is the number of keys in the input dictionary and ‘m’ is the maximum number of elements in any list in the dictionary. This is because we iterate through each key-value pair in the input dictionary once and for each element in the lists, we check the corresponding set in the dictionary ‘d’, which has a maximum size of ‘m’.

Auxiliary Space:
The space complexity of this code is O(n * m), where ‘n’ is the number of keys in the input dictionary and ‘m’ is the maximum number of elements in any list in the dictionary. This is because we create a dictionary ‘d’ with ‘n’ keys and each key has a set with maximum size ‘m’. We also create a new dictionary ‘res’ with ‘n’ keys and the maximum length of any list in ‘res’ is also ‘m’.

 



Similar Reads

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
8 min read
Python | Remove duplicate dictionaries from nested dictionary
Given a nested dictionary, the task is to remove duplicate dictionaries from the dictionary. Given below are few methods to complete the given task. Method #1: Using Naive Method C/C++ Code # Python code to demonstrate # for removing duplicate values from dictionary # initialising dictionary ini_dict = {'a':{'b':1, 'c':2}, 'b':{'b':1, 'c':2}, 'c':{
6 min read
Python | Find keys with duplicate values in dictionary
Given a dictionary, the task is to find keys with duplicate values. Let's discuss a few methods for the same. Method #1: Using Naive approach In this method first, we convert dictionary values to keys with the inverse mapping and then find the duplicate keys C/C++ Code # Python code to demonstrate # finding duplicate values from a dictionary # init
4 min read
Python - Values frequency across Dictionaries lists
Given two list of dictionaries, compute frequency corresponding to each value in dictionary 1 to second. Input : test_list1 = [{"Gfg" : 6}, {"best" : 10}], test_list2 = [{"a" : 6}, {"b" : 10}, {"d" : 6}}] Output : {'Gfg': 2, 'best': 1} Explanation : 6 has 2 occurrence in 2nd list, 10 has 1. Input : test_list1 = [{"Gfg" : 6}], test_list2 = [{"a" : 6
6 min read
Python - Append Dictionary Keys and Values ( In order ) in dictionary
Given a dictionary, perform append of keys followed by values in list. Input : test_dict = {"Gfg" : 1, "is" : 2, "Best" : 3} Output : ['Gfg', 'is', 'Best', 1, 2, 3] Explanation : All the keys before all the values in list. Input : test_dict = {"Gfg" : 1, "Best" : 3} Output : ['Gfg', 'Best', 1, 3] Explanation : All the keys before all the values in
5 min read
Python program to update a dictionary with the values from a dictionary list
Given a dictionary and dictionary list, update the dictionary with dictionary list values. Input : test_dict = {"Gfg" : 2, "is" : 1, "Best" : 3}, dict_list = [{'for' : 3, 'all' : 7}, {'and' : 1, 'CS' : 9}] Output : {'Gfg': 2, 'is': 1, 'Best': 3, 'for': 3, 'all': 7, 'and': 1, 'CS': 9} Explanation : All dictionary keys updated in single dictionary. I
8 min read
Python - Filter dictionary values in heterogeneous dictionary
Sometimes, while working with Python dictionaries, we can have a problem in which we need to filter out certain values based on certain conditions on a particular type, e.g all values smaller than K. This task becomes complex when dictionary values can be heterogeneous. This kind of problem can have applications across many domains. Let's discuss c
6 min read
Python - Remove digits from Dictionary String Values List
Given list of dictionaries with string list values, remove all the numerics from all strings. Input: test_dict = {'Gfg' : ["G4G is Best 4", "4 ALL geeks"], 'best' : ["Gfg Heaven", "for 7 CS"]} Output: {'Gfg': ['GG is Best ', ' ALL geeks'], 'best': ['Gfg Heaven', 'for CS']} Explanation: All the numeric strings are removed. Input: test_dict = {'Gfg'
10 min read
Python - Multiplication across Like Keys Value list elements
Given two dictionaries with value lists, perform element wise like keys multiplication. Input : test_dict1 = {"Gfg" : [4, 6], "Best" : [8, 6], "is" : [9, 3]}, test_dict2 = {"Gfg": [8, 4], "Best" : [6, 3], "is" : [9, 8]} Output : {'Gfg': [32, 24], 'Best': [48, 18], 'is': [81, 24]} Explanation : 4 * 8 = 32, 6 * 4 = 24 and so on, hence new list value.
9 min read
Python program to concatenate every elements across lists
Given 2 lists, perform concatenations of all strings with each other across list. Input : test_list1 = ["gfg", "is", "best"], test_list2 = ["love", "CS"] Output : ['gfg love', 'gfg CS', 'is love', 'is CS', 'best love', 'best CS'] Explanation : All strings are coupled with one another. Input : test_list1 = ["gfg", "best"], test_list2 = ["love", "CS"
4 min read
Practice Tags :