We can use loop or dictionary comprehension to remove duplicates from the dictionary in Python. While removing a duplicate value from the dictionary the keys are also removed in the process.
If you don’t care about retaining the original order then set(my_list)
will remove all duplicates.
Example remove duplicates from the dictionary in Python
Simple example code.
Using for loop
This is the brute force method, where add first of occurred value in a variable, and remove it if it repeats.
dict1 = {'A': 20, 'B': 15, 'C': 20, 'D': 10, 'E': 20}
temp = []
res = dict()
for key, val in dict1.items():
if val not in temp:
temp.append(val)
res[key] = val
print(res)
Output:
Using dictionary comprehension
This method does the same as above but it is a shorthand.
dict1 = {'A': 20, 'B': 15, 'C': 20, 'D': 10, 'E': 20}
temp = {val: key for key, val in dict1.items()}
res = {val: key for key, val in temp.items()}
print(res)
Output: {‘E’: 20, ‘B’: 15, ‘D’: 10}
Write a Python program to remove duplicates from Dictionary
Here is a python example that shows how to remove duplicate values from a dictionary.
student_data = {'id1':
{'name': ['Sara'],
'class': ['V'],
'subject_integration': ['english, math, science']
},
'id2':
{'name': ['David'],
'class': ['V'],
'subject_integration': ['english, math, science']
},
'id3':
{'name': ['Sara'],
'class': ['V'],
'subject_integration': ['english, math, science']
},
'id4':
{'name': ['Surya'],
'class': ['V'],
'subject_integration': ['english, math, science']
},
}
res = {}
for key, value in student_data.items():
if value not in res.values():
res[key] = value
print(res)
Do comment if you have any doubts and suggestions on this Python dictionary tutorial.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Hi Rohit,
Thank you for the detailed explanation. I have a question about the example script. How can I compare only 2 keys in a list. for example: remove duplicates only if name and subject_integration is same and class can be different.
Regards,
Bharath.
array1 = array1.filter(function(val) {
return array2.indexOf(val) == -1;
});
Or, with the availability of ES6:
array1 = array1.filter(val => !array2.includes(val));