Remove duplicates from a dictionary in Python

When you are working with Python dictionaries, you may come across situations where you have duplicate values present in the dictionary. This can, in some situations, hinder your program. In such a case, you might need to evict the duplicates in your dictionary.
In this tutorial, you will learn how to remove duplicates from a Python dictionary.

Using looping

The most basic approach is to go through each of the items in the dictionary and then add the first appearance of each item to a new resultant dictionary. In better words, we simply keep track of the dictionary and remove any value that repeats.
To understand this approach better, let us go through the example below:

eg_dic={1:1,2:2,3:2,4:3}
print("The original dictionary is:",eg_dic)
temp=[]
res={}
for key,val in eg_dic.items():
    if val not in temp:
        temp.append(val)
        res[key]=val
print("The dictionary after removing the duplicates is:",res)
The original dictionary is: {1: 1, 2: 2, 3: 2, 4: 3}
The dictionary after removing the duplicates is: {1: 1, 2: 2, 4: 3}

Dictionary Comprehension Method to remove duplicates from a dictionary in Python

This is just a shorthand method for the same operations performed above. Example codes for the same are as shown below:

eg_dic={1:1,2:2,3:2,4:3}
print("The original dictionary is:",eg_dic)
temp={val:key for key,val in eg_dic.items()}
res={val:key for key, val in temp.items()}
print("The dictionary after removing the duplicates is:",res)
The original dictionary is: {1: 1, 2: 2, 3: 2, 4: 3}
The dictionary after removing the duplicates is: {1: 1, 3: 2, 4: 3}
eg_dic={1:1,2:2,3:2,4:3}
print("The original dictionary is:",eg_dic)
for key,val in eg_dic.items():
    if val not in res.values():
        res[key]=val
print("The dictionary after removing the duplicates is:",res)
The original dictionary is: {1: 1, 2: 2, 3: 2, 4: 3}
The dictionary after removing the duplicates is: {1: 1, 3: 2, 4: 3}

Well, if you don’t care about retaining the order of the list, you can use the set() option. This casts the dictionary into a set. In sets, an element can appear only once and hence will remove all duplicates.

To read more on the same, click Using set on Dictionary keys in Python

Leave a Reply

Your email address will not be published. Required fields are marked *