Python remove duplicates from dictionary | Example code

We can use loop or dictionary comprehension to remove duplicates from the dictionary in Python. While removing a duplicates 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:

Python remove duplicates from dictionary

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.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.