Given a list of dictionaries, remove all the dictionaries which are duplicate with respect to K key.
Input : test_list = [{“Gfg” : 6, “is” : 9, “best” : 10}, {“Gfg” : 8, “is” : 11, “best” : 10}, {“Gfg” : 2, “is” : 16, “best” : 10}], K = “best”
Output : [{“Gfg” : 6, “is” : 9, “best” : 10}]
Explanation : All keys have 10 value, only 1st record is retained.Input : test_list = [{“Gfg” : 6, “is” : 9, “best” : 10}, {“Gfg” : 8, “is” : 11, “best” : 12}, {“Gfg” : 2, “is” : 16, “best” : 15}], K = “best”
Output : [{“Gfg” : 6, “is” : 9, “best” : 10}, {“Gfg” : 8, “is” : 11, “best” : 12}, {“Gfg” : 2, “is” : 16, “best” : 15}]
Explanation : All values of “best” are unique, hence no removal of dictionaries.
Method : Using loop
This is brute way in which this task can be performed. In this, we iterate for each dictionary and memoize the Key, if similar key’s same value occur, then that dictionary is avoided in resultant list of dictionaries.
Python3
# Python3 code to demonstrate working of # Remove Duplicate Dictionaries characterized by Key # Using loop # initializing lists test_list = [{"Gfg" : 6, "is" : 9, "best" : 10}, {"Gfg" : 8, "is" : 11, "best" : 19}, {"Gfg" : 2, "is" : 16, "best" : 10}, {"Gfg" : 12, "is" : 1, "best" : 8}, {"Gfg" : 22, "is" : 6, "best" : 8}] # printing original list print("The original list : " + str(test_list)) # initializing Key K = "best" memo = set() res = [] for sub in test_list: # testing for already present value if sub[K] not in memo: res.append(sub) # adding in memo if new value memo.add(sub[K]) # printing result print("The filtered list : " + str(res)) |
The original list : [{‘Gfg’: 6, ‘is’: 9, ‘best’: 10}, {‘Gfg’: 8, ‘is’: 11, ‘best’: 19}, {‘Gfg’: 2, ‘is’: 16, ‘best’: 10}, {‘Gfg’: 12, ‘is’: 1, ‘best’: 8}, {‘Gfg’: 22, ‘is’: 6, ‘best’: 8}]
The filtered list : [{‘Gfg’: 6, ‘is’: 9, ‘best’: 10}, {‘Gfg’: 8, ‘is’: 11, ‘best’: 19}, {‘Gfg’: 12, ‘is’: 1, ‘best’: 8}]
Recommended Posts:
- Python - Convert Dictionaries List to Order Key Nested dictionaries
- Python | Remove duplicate dictionaries from nested dictionary
- Python | Sort Python Dictionaries by Key or Value
- Python | Combine the values of two dictionaries having same key
- Python | Sum list of dictionaries with same key
- Python | Get values of particular key in list of dictionaries
- Python | Segregating key's value in list of dictionaries
- Python - Sort dictionaries list by Key's Value list index
- Python - Add custom values key in List of dictionaries
- Python - Extract Key's value from Mixed Dictionaries List
- Python - Remove empty value types in dictionaries list
- Python - Extract target key from other key values
- Python - Filter key's value from other key
- Python - Extract Key's Value, if Key Present in List and Dictionary
- Python | Remove duplicate tuples from list of tuples
- Python | Remove unordered duplicate elements from a list
- Python | Remove tuples having duplicate first value from given list of tuples
- Python | Remove duplicate lists in tuples (Preserving Order)
- Python - Remove all duplicate occurring tuple records
- Python - Remove duplicate words from Strings in List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

