How to remove duplicate values from a Python Dictionary

In this Python article, we will learn how to remove the duplicate/same values corresponding to different keys in Python Dictionary using some examples. We will learn various methods to remove duplicate values in Python Dictionary.

For example: Below is the Python dictionary with 3-duplicate values in it:

Employees = {
    'rob': 50,
    'john': 48,
    'tim': 50,
    'sam': 38,
    'rosy': 50,
    'dave': 22,
}
how to remove duplicate values from dictionary showing three duplicate values

We can remove these duplicate values from a dictionary in Python by using the below 7 methods:

  • By using For Loop
  • By using a dictionary comprehension
  • By using the setdefault() method
  • By using values() and set() methods
  • By using collections.defaultdict()
  • By using List comprehension
  • By using dict.fromkeys() and values()

Remove duplicate values from a Python Dictionary

So, Let’s learn different ways to remove Python dictionaries’ duplicate values with a few examples.

Method-1: By using Python For Loop

This is the basic method to remove the duplicate value from the Python dictionaries.

First, we will create a new dictionary in Python that will contain all the results we want and a temporary list that will keep track of all values. Then we will loop through the dictionary and check if the value is already in the list created or not. If not, then only we will add it to the new Python dictionary.

In the following example, I have created a Python Dictionary that has the key as names of people and value as bid price given by them. We will select every person in the auction who has first entered a unique bid price.

# initial dictionary
person_name_with_bid_price = {
   'sam': 1000,
   'tom': 500,
   'rob': 1000,
   'john': 600,
   'rosy': 750,
   'lily': 500
}
# printing the actual dictionary:
print("participant's name with their bid price: ", person_name_with_bid_price)

temp_list = []
unique_people = {}

# using loop
for key, value in person_name_with_bid_price.items():
    if value not in temp_list:
        temp_list.append(value)
        unique_people[key] = value

# printing result
print("unique people names with their prices are: ", unique_people)

The output of the above code will be:

participant's name with their bid price: {'sam': 1000, 'tom': 500, 'rob': 1000, 'john': 600, 'rosy': 750, 'lily': 500}
unique people names with their bid prices are: {'sam': 1000, 'tom': 500, 'john': 600, 'rosy': 750}
how to remove duplicate values from dictionary using for loop

This way we can remove the duplicate values in the Python dictionary using for loop.

Method-2: By using a Python dictionary comprehension

In this method, we will use the dictionary comprehension method in Python.

Here we will create two Python dictionaries, the first one will have the reverse order of the initial dictionary i.e., the key-value pair will be interchanged. And as we know, the dictionary keys in Python are always unique, so when we will reverse the order the duplicate values from the initial dictionary will automatically get eliminated.

And in the second one, we will again repeat the same process with the first created Python dictionary so that, we can get the final result as we wanted.

In the following code of Python, here, we have the dictionary with keys as names of employees and values with the allotted seat number of a bus. We will be selecting one-one employee from each seat allotted.

# seat allotment
# initial dictionary
employees_name_with_seat_number = {
   'sam': 10,
   'tom': 13,
   'rob': 10,
   'john': 16,
   'rosy': 14,
   'lily': 15,
   'joey': 12,
   'dave': 16
}
# printing the actual dictionary:
print("employee's name with their allotted seat number: \n",employees_name_with_seat_number)

# using dictionary comprehension
temp = {value: key for key, value in employees_name_with_seat_number.items()}
result = {value: key for key, value in temp.items()}

# printing result
print("one employee from each allotted seat is: \n", result)

The output of the above code is:

employee's name with their allotted seat number:
{'sam': 10, 'tom': 13, 'rob': 10, 'john': 16, 'rosy': 14, 'lily': 15, 'joey': 12, 'dave': 16}
one employee from each allotted seat is:
{'rob': 10, 'tom': 13, 'dave': 16, 'rosy': 14, 'lily': 15, 'joey': 12}
remove duplicate values from dictionary in python using dict. comprehension

Through this process, we will be able to remove duplicate values in a dictionary using dictionary comprehension.

Read How to copy specific keys in Python Dictionary

Method-3: By using the Python setdefault() method

In this method, we will use the setdefault() method in Python. The setdefault() method returns the value of the item with the specified key.

Here we will create a temporary Python dictionary where we will store the values after using the setdefault() method. And will create another dictionary to reverse the key-value pair as a result.

In the following example, we are playing a musical chair game, where no two people can sit on the same chair number. So, we will remove one person from each repeated chair number.

# musical chair
# initial dictionary
people_with_chair_number = {
   'sam': 1,
   'tom': 5,
   'rob': 1,
   'john': 6,
   'rosy': 7,
   'lily': 5,
   'david': 2
}
print("participant's name with their chair number: \n", people_with_chair_number)

temp_dict = {}

# using setdefault() method
for key, value in people_with_chair_number.items():
    temp_dict.setdefault(value, key)

result = dict((value,key) for key, value in temp_dict.items())

print("people going to next round are: ", result)

The output of the above code is:

participant's name with their chair number:
{'sam': 1, 'tom': 5, 'rob': 1, 'john': 6, 'rosy': 7, 'lily': 5, 'david': 2}
people going to next round are: {'sam': 1, 'tom': 5, 'john': 6, 'rosy': 7, 'david': 2}
C:\Users\USER\Documents\TSinfo credential\Images\remove duplicate values from dictionary in python using dict. setdefault().jpg

This way in Python we can remove the duplicate values in a dictionary using setdefault().

Method-4: By using values() and set() methods in Python

In this method, we will use the values() and set() methods. As the Python values() method returns the values from a Python dictionary in the form of a dictionary and the Python set() can only contain unique elements.

Firstly, we will create a set that will contain all the unique values from the initial Python dictionary. Then, we will create an empty Python dictionary to store results.

In the following code, here, we have singer names nominated US. But, we want only one singer from each city who can sing. We create a Python dictionary with the name of the singer as the key and their city as the value and, we will select one singer from each city:

# singers
# initial dictionary
singers = {
   'Taylor': 'Texas',
   'MJ': 'Indiana',
   'Katy': 'California',
   'Selena': 'Texas',
   'Lady': 'New York',
   'Axl': 'Indiana'
}
# printing the initial dictionary:
print("singers names with their city: \n", singers)

# using set() and values()
temp_set = set(singers.values())

result = {}

for value in temp_set:
    for key in singers.keys():
        if singers[key] == value:
            result[key] = value
            break

print("selected singers names from each city are: \n", result)

The output of the above code is:

singers names with their city:
{'Taylor': 'Texas', 'MJ': 'Indiana', 'Katy': 'California', 'Selena': 'Texas', 'Lady': 'New York', 'Axl': 'Indiana'}
selected singers names from each city are:
{'Taylor': 'Texas', 'Lady': 'New York', 'MJ': 'Indiana', 'Katy': 'California'}
remove duplicate values from Python dictionary using values() and set()

This is the way we can remove the duplicate from the dictionary using values() and set() in Python.

Read How to update duplicate keys in Python Dictionary

Method-5: By using Python collections.defaultdict()

In this method, we import the defaultdict module from the collections library to remove duplicate values from a dictionary in Python.

Here, we will create a Python temporary defaultdict with the default value as an empty list. Then, we will add the keys to the empty list (one by one)of the values in the defaultdict object.

We will create a dictionary that will take the elements of the defaultdict object and will select the 1st value of each list. Lastly, we will reverse the key-value pair of the created dictionary into the result dictionary.

In this code, suppose there is a hotel that has single and double rooms. And all guests came to the counter to receive keys for the room and there is a huge gathering like a fish market. So, to remove this gathering, we are calling one guest from each room of the hotel to collect the key.

# importing the module
from collections import defaultdict

# hotel room
# initial dictionary
guests = {
   'sam': 101,
   'tom': 105,
   'rob': 102,
   'john': 103,
   'rosy': 105,
   'lily': 104,
   'david': 102
}
# printing the actual dictionary:
print("Guests in the hotel rooms are: \n", guests)

# using defaultdict()
temp_ddict = defaultdict(list)

for key, value in guests.items():
    temp_ddict[value].append(key)

temp_dic = {key:value[0] for key, value in temp_ddict.items()}

result = dict((value, key) for key, value in temp_dic.items())

# printing result
print("One guest from each rooms are: \n", result)

The output of the above-written code is:

Guests in the hotel rooms are:
{'sam': 101, 'tom': 105, 'rob': 102, 'john': 103, 'rosy': 105, 'lily': 104, 'david': 102}
One guest from each rooms are:
{'sam': 101, 'tom': 105, 'rob': 102, 'john': 103, 'lily': 104}
remove duplicate values from a Python dictionary using defaultdict

This way we can remove the duplicate values in the Python dictionary using defaultdict().

Method-6: By using Python List comprehension

In this, we simply use the list comprehension method in Python.

Here, we will create one Python temporary list and one dictionary to save results. We will check the values of the initial Python dictionary is not present then we append it to the list and store the corresponding key-value pair in the result dictionary.

In this code, In a theater, people came to watch a stand-up comedy show. But some people started fighting for the tokens. So, we want to check whether the tokens are valid or not. As there should be no two tokens of the same number.

So, we have a Python dictionary with names as keys and their token numbers as values, we are checking the valid tokens.

# valid tokens
# initial dictionary
peoples_with_token_number = {
   'kim': 11,
   'sam': 15,
   'rob': 12,
   'kate': 13,
   'jerry': 15,
   'tom': 14,
   'david': 12
}
# printing the actual dictionary:
print("peoples with their token numbers: \n", peoples_with_token_number)

# using list comprehension
temp_list = []
result_dict ={}

for key, value in peoples_with_token_number.items():
    if value not in temp_list:
        result_dict[key] = value
        temp_list.append(value)

# printing the result
print("the people with valid tokens are: \n", result_dict)

The output of the above-written code is:

peoples with their token numbers:
{'kim': 11, 'sam': 15, 'rob': 12, 'kate': 13, 'jerry': 15, 'tom': 14, 'david': 12}
the people with valid tokens are:
{'kim': 11, 'sam': 15, 'rob': 12, 'kate': 13, 'tom': 14}
remove duplicate values from dictionary IN PYTHON using list comprehension

This way we can remove duplicate values from a Python dictionary using list comprehension.

Method-7: By using dict.fromkeys() and values()

In this method, we will be using Python dict.fromkeys() and values(). The dict.fromkeys() is used to create a Python dictionary with keys from an iterable and values as None(as default).

Here, we are creating a dictionary with keys from the values of the initial dictionary using dict.fromkeys() and values().

We can then use a Python loop to iterate over the initial dictionary and set the values of the new dictionary to the corresponding key if the value has not already been there.

In this code, people came to visit a doctor who will be available for two hours that day. So, the compounder has decided to allot one 20-minute slot to each patient. But, he gave some people repeated slot numbers.

Now, we have to select one person from each slot number. We will create a dictionary with names as keys and slot numbers as values:

# solt number
# initial dictionary
peoples_with_slot_number = {
   'kim': 11,
   'sam': 15,
   'rob': 12,
   'kate': 13,
   'jerry': 15,
   'tom': 14,
   'david': 12
}
# printing the actual dictionary:
print("people with their slot numbers: \n", peoples_with_slot_number)

# using dict.fromkeys() and values()
temp_dict = dict.fromkeys(peoples_with_slot_number.values())
result_dict = {}
for key, value in peoples_with_slot_number.items():
     if temp_dict[value] is None:
         temp_dict[value] = key
         result_dict[key] = value

# printing the result
print("the people with lone slot numbers are: \n", result_dict)

The output for the above-mentioned code is:

People with their slot numbers:
{'kim': 11, 'sam': 15, 'rob': 12, 'kate': 13, 'jerry': 15, 'tom': 14, 'david': 12}
the people with lone slot numbers are:
{'kim': 11, 'sam': 15, 'rob': 12, 'kate': 13, 'tom': 14}
remove duplicate values from dictionary using dict.fromkeys and values

Through this process, we can remove duplicate values from a dictionary using dict.fromkeys() and values().

Conclusion:

In this article, we have learned about how to remove duplicate values in a Dictionary in Python. We came across seven different ways to do so, which can be very useful in data analysis and cleaning.

You may like to read: