Python - Remove Duplicate Dictionaries characterized by Key
Last Updated :
14 Apr, 2023
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))
Output
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}]
METHOD 2 : use list comprehension.
Here are the steps to implement it:
- Initialize the input list:
- Specify the key to be used for identifying duplicates
- Use list comprehension to filter out duplicates:
- This line of code creates a set of tuples where each tuple represents a dictionary in the input list. The dict() function is then used to convert each tuple back into a dictionary, and the resulting list contains only unique dictionaries.
- Print the filtered list:
Python3
# Initializing the input list
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}]
# Specify the key to be used for identifying duplicates
K = "best"
# Use list comprehension to filter out duplicates
res = [dict(t) for t in {tuple(d.items()) for d in test_list}]
# Print the filtered list
print("The filtered list : " + str(res))
OutputThe filtered list : [{'Gfg': 2, 'is': 16, 'best': 10}, {'Gfg': 8, 'is': 11, 'best': 19}, {'Gfg': 6, 'is': 9, 'best': 10}, {'Gfg': 22, 'is': 6, 'best': 8}, {'Gfg': 12, 'is': 1, 'best': 8}]
The time complexity of this approach is O(nlogn) due to the use of sets and tuples.
Auxiliary space complexity is O(n) because we are creating a new list of dictionaries.
Similar Reads
Remove Duplicate Dictionaries from Nested Dictionary - Python We are given a nested dictionary we need to remove the duplicate dictionaries from the nested dictionary. For example we are given a nested dictionary d = {'key1': [{'a': 1}, {'b': 2}, {'a': 1}], 'key2': [{'x': 3}, {'y': 4}]} we need to remove the duplicate dictionary from this dictionary so output
4 min read
Remove Duplicity from a Dictionary - Python We are given a dictionary and our task is to remove duplicate values from it. For example, if the dictionary is {'a': 1, 'b': 2, 'c': 2, 'd': 3, 'e': 1}, the unique values are {1, 2, 3}, so the output should be {'a': 1, 'b': 2, 'd': 3}.Using a loopThis method uses a loop to iterate through dictionar
3 min read
Python Remove Item from Dictionary by Key Dictionaries in Python store data as key-value pairs. Often, we need to remove a specific key-value pair to modify or clean the dictionary. For instance, consider the dictionary d = {'a': 1, 'b': 2, 'c': 3}; we might want to remove the key 'b'. Let's explore different methods to achieve this.Using d
3 min read
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.
8 min read
Python - Remove duplicate values across Dictionary Values Dictionaries often contain lists or sets as their values and sometimes we want to remove duplicate values across all dictionary keys. For example, consider the dictionary d = {'a': [1, 2, 3], 'b': [3, 4, 5], 'c': [5, 6]}. The number 3 appears under both 'a' and 'b', and 5 appears under both 'b' and
3 min read