Python – Dictionaries with Unique Value Lists

30 Aug, 2020
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 liststest_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 listprint("The original list : " + str(test_list))  # Using loop to iterate through elements# result array to also keep track of already occurred elementsres = []for sub in test_list:    if sub not in res:        res.append(sub)  # printing result print("List after duplicates removal : " + str(res))
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 #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 liststest_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 listprint("The original list : " + str(test_list))  # list comprehension to encapsulate logic in one linerres = [][res.append(val) for val in test_list if val not in res]  # printing result print("List after duplicates removal : " + str(res))
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]}]

Python list-programs
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python program to convert a list to string
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Convert a list to dictionary
Python | Convert string dictionary to dictionary