Python – Dictionaries with Unique Value Lists
Last Updated :
13 Apr, 2023
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
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 ]}]
print ( "The original list : " + str (test_list))
res = []
for sub in test_list:
if sub not in res:
res.append(sub)
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
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 ]}]
print ( "The original list : " + str (test_list))
res = []
[res.append(val) for val in test_list if val not in res]
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
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 ,)}]
print ( "The original list : " + str (test_list))
unique_set = set ()
res = []
for d in test_list:
d_tuple = tuple ( sorted (d.items()))
if d_tuple not in unique_set:
unique_set.add(d_tuple)
res.append(d)
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
Share your thoughts in the comments
Please Login to comment...