How to Remove Duplicates from a Dictionary in Python?

The term duplicates mean the elements which are repeated.

When working with Python dictionaries, you may encounter circumstances where there are duplicate values in the dictionary. In some cases, this can be harmful to your software. In this instance, you may need to remove the duplicates from your dictionary.

When working with Python dictionaries, we may encounter a problem in which we need to delete all duplicate values from the dictionary, and we are unconcerned whether some keys are removed in the process. This type of application can be seen in both school programming and day-to-day programming.

The below-given ways can be used to remove duplicates from a dictionary in  python:

  • Using the for loop
  • Using the Dictionary Comprehension method.

Examples:

Example1:

Input:

Given dictionary = {1: 100, 2: 90, 3: 80, 4: 100, 5:80}

Output:

The given dictionary: {1: 100, 2: 90, 3: 80, 4: 100, 5: 80}
The result dictionary after the removal of duplicates: {1: 100, 2: 90, 3: 80}

Example2:

Here, we are printing the value: key format

Input:

Given dictionary = {10:"hello", 20:"Python-programs", 30:"good", 40:"morning", 50:"hello"}

Output:

The given dictionary:
{10: 'hello', 20: 'Python-programs', 30: 'good', 40: 'morning', 50: 'hello'}
The result dictionary after the removal of duplicates:
{'hello': 50, 'Python-programs': 20, 'good': 30, 'morning': 40}

Removing Duplicates from a Dictionary in Python

Below are the method to remove the duplicates from the given dictionary in Python:

Method #1: Using For loop

Approach:

  • Give the dictionary as static input and store it in a variable.
  • Print the given dictionary
  • Take an empty list and store it in aother variable.
  • Create a new empty dictionary and store it in aother variable.
  • Loop in the key, value of the given dictionary using the for loop and the items() functions
  • Check if the corresponding value is not present in the above created new list using the if conditional statement
  • If it is true then append that value to the above list
  • Here it appends the value to the new list if it is a unique element
  • Assign this unique key and value to the new dictionary
  • Print the result dictionary after the removal of duplicates.
  • The Exit of the Program.

Below is the implementation:

# Give the dictionary as static input and store it in a variable.
gvn_dict ={1: 100, 2: 90, 3: 80, 4: 100, 5:80}
# Print the given dictionary
print("The given dictionary:",gvn_dict)
# Take an empty list and store it in aother variable.
new_lst =[]
# Create a new empty dictionary and store it in aother variable.
output_dict ={}
# Loop in the key, value of the given dictionary using the for loop and the items() functions
for key, value in gvn_dict.items():
    # Check if the corresponding value is not present in the above created new list using the 
    # if conditional statement
    if value not in new_lst:
        # If it is true then append that value to the above list 
        # Here it appends the value to the new list if it is a unique element
        new_lst.append(value)
        # Assign this unique key and value to the new dictionary
        output_dict[key] = value
# Print the result dictionary after the removal of duplicates
print("The result dictionary after the removal of duplicates:",output_dict)

Output:

The given dictionary: {1: 100, 2: 90, 3: 80, 4: 100, 5: 80}
The result dictionary after the removal of duplicates: {1: 100, 2: 90, 3: 80}

Method #2: Using Dictionary Comprehension method

This is simply a shortcut for the operations described above. Here we print the value first followed by a key.

Approach:

  • Give the dictionary as static input and store it in a variable.
  • Print the given dictionary
  • Create another dictionary with key as values of original dictionary and values as keys of the original dictionary using dictionary comprehension.
  • Print the result dictionary after the removal of duplicates.
  • The Exit of the Program.

Below is the implementation:

# Give the dictionary as static input and store it in a variable.
gvn_dict ={10:"hello", 20:"Python-programs", 30:"good", 40:"morning", 50:"hello"}
# Print the given dictionary
print("The given dictionary:\n",gvn_dict)
# Create another dictionary with key as values of original dictionary and
# values as keys of the original dictionary using dictionary comprehension
output_dict = {value:key for key, value in gvn_dict.items()}
# Print the result dictionary after the removal of duplicates
print("The result dictionary after the removal of duplicates:\n",output_dict)

Output:

The given dictionary:
{10: 'hello', 20: 'Python-programs', 30: 'good', 40: 'morning', 50: 'hello'}
The result dictionary after the removal of duplicates:
{'hello': 50, 'Python-programs': 20, 'good': 30, 'morning': 40}