Open In App

Python – Dictionaries with Unique Value Lists

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

Given List of dictionaries with list values, extract unique dictionaries.

Input : [{‘Gfg’: [2, 3], ‘is’ : [7, 8], ‘best’ : [10]}, {‘Gfg’: [2, 3], ‘is’ : [7, 8], ‘best’ : [10]}] 
Output : [{‘Gfg’: [2, 3], ‘is’: [7, 8], ‘best’: [10]}] 
Explanation : Both are similar dictionaries, and hence 1 is removed. 

Input : [{‘Gfg’: [2, 3], ‘is’ : [7, 8], ‘best’ : [10]}, {‘Gfg’: [2, 3], ‘is’ : [7, 8], ‘best’ : [10, 11]}] 
Output : [{‘Gfg’: [2, 3], ‘is’: [7, 8], ‘best’: [10]}, {‘Gfg’: [2, 3], ‘is’: [7, 8], ‘best’: [10, 11]}] 
Explanation : None duplicate.

Method #1 : Using loop

This is one of the ways in which this task can be performed. In this, we iterate for each dictionary and memoize it, and prevent it from adding to result. 

Python3




# Python3 code to demonstrate working of
# Unique Value Lists Dictionaries
# Using loop
 
# initializing lists
test_list = [{'Gfg':  [2, 3], 'is' : [7, 8], 'best' : [10]},
             {'Gfg':  [2, 3], 'is' : [7], 'best' : [10]},
             {'Gfg':  [2, 3], 'is' : [7, 8], 'best' : [10]}]
 
# printing original list
print("The original list : " + str(test_list))
 
# Using loop to iterate through elements
# result array to also keep track of already occurred elements
res = []
for sub in test_list:
    if sub not in res:
        res.append(sub)
 
# printing result
print("List after duplicates removal : " + str(res))


Output

The original list : [{'Gfg': [2, 3], 'is': [7, 8], 'best': [10]}, {'Gfg': [2, 3], 'is': [7], 'best': [10]}, {'Gfg': [2, 3], 'is': [7, 8], 'best': [10]}]
List after duplicates removal : [{'Gfg': [2, 3], 'is': [7, 8], 'best': [10]}, {'Gfg': [2, 3], 'is': [7], 'best': [10]}]

Time complexity: O(n), where n is the length of the test_list. The  loop takes O(n) time
Auxiliary Space: O(n), extra space of size n is required

Method #2 : Using list comprehension

This is yet another way in which this task can be performed. In this, similar approach is employed as above, just the difference of encapsulating result in list comprehension for one-liner. 

Python3




# Python3 code to demonstrate working of
# Unique Value Lists Dictionaries
# Using list comprehension
 
# initializing lists
test_list = [{'Gfg':  [2, 3], 'is' : [7, 8], 'best' : [10]},
             {'Gfg':  [2, 3], 'is' : [7], 'best' : [10]},
             {'Gfg':  [2, 3], 'is' : [7, 8], 'best' : [10]}]
 
# printing original list
print("The original list : " + str(test_list))
 
# list comprehension to encapsulate logic in one liner
res = []
[res.append(val) for val in test_list if val not in res]
 
# printing result
print("List after duplicates removal : " + str(res))


Output

The original list : [{'Gfg': [2, 3], 'is': [7, 8], 'best': [10]}, {'Gfg': [2, 3], 'is': [7], 'best': [10]}, {'Gfg': [2, 3], 'is': [7, 8], 'best': [10]}]
List after duplicates removal : [{'Gfg': [2, 3], 'is': [7, 8], 'best': [10]}, {'Gfg': [2, 3], 'is': [7], 'best': [10]}]

Method 3: Using a set and tuple.

Step-by-step approach:

  • Create an empty set to hold the unique values.
  • Loop through each dictionary in the list.
    • Convert the dictionary to a tuple, since sets can’t contain dictionaries.
    • Check if the tuple is in the set of unique values. If it’s not, add it to the set and append the original dictionary to the result list.
  • Print the result list.

Python3




# Python3 code to demonstrate working of
# Unique Value Lists Dictionaries
# Using set and tuple
 
# initializing lists
test_list = [{'Gfg':  (2, 3), 'is' : (7, 8), 'best' : (10,)},
             {'Gfg':  (2, 3), 'is' : (7,), 'best' : (10,)},
             {'Gfg':  (2, 3), 'is' : (7, 8), 'best' : (10,)}]
 
# printing original list
print("The original list : " + str(test_list))
 
# set to hold unique values
unique_set = set()
 
# result list
res = []
 
# loop through each dictionary
for d in test_list:
    # convert dictionary to tuple
    d_tuple = tuple(sorted(d.items()))
    # check if tuple is in unique set
    if d_tuple not in unique_set:
        # if not, add tuple to set and original dictionary to result list
        unique_set.add(d_tuple)
        res.append(d)
 
# printing result
print("List after duplicates removal : " + str(res))


Output

The original list : [{'Gfg': (2, 3), 'is': (7, 8), 'best': (10,)}, {'Gfg': (2, 3), 'is': (7,), 'best': (10,)}, {'Gfg': (2, 3), 'is': (7, 8), 'best': (10,)}]
List after duplicates removal : [{'Gfg': (2, 3), 'is': (7, 8), 'best': (10,)}, {'Gfg': (2, 3), 'is': (7,), 'best': (10,)}]

Time complexity: O(n log n) due to sorting the dictionaries into tuples.
Auxiliary space: O(n) for storing the unique values in a set and the result list.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads