
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove Duplicate Elements from a Dictionary in Python
In this article, we will be discussing how to remove duplicate elements from a dictionary in Python. A dictionary is a data structure that stores key-value pairs, and it is an essential data type to understand when learning Python. However, there may be cases where we have a dictionary with duplicate elements, and we want to remove them to clean up the data.
We can declare a dictionary in the following way ?
thisdict = { "brand": "Ford" , "model": "Mustang" , "year": 1964 }
However, a dictionary can contain duplicate value for different keys, but we might want to have only unique values to exist in our dictionary.
In this article we will look into 2 different methods to remove duplicates elements from a dictionary.
By iterating through the dictionary
This is a very basic approach towards the problem. First we will create a new dictionary to contain all the unique values in it and we also create a list to keep the values as we encounter them, then we will simply iterate through the dictionary and check if the value we have encountered already exists in the list we created and if it already exists we do not add that value to that dictionary and if the value encountered doesn't exist in our dictionary we will add it.
Algorithm
Create a new empty dictionary and a new list.
Start the iteration on the given dictionary.
Check if the value of current iteration exist in the newly created list.
If it doesn't exist we will add the value in the new dictionary and also in the list else we go to next iteration.
Print the newly created dictionary which will contain all the unique elements from the original dictionary.
Example
In the following code, we will be implementing the above algorithm and printing the dictionary with all the unique elements.
emp_data = { '001':'Ramu', '002':'Radha', '003':'Ramu', '004':'Raghav'} print("Contents of the dictionary: " + str(emp_data)) temp = [] resultant_dictionary = dict() for key, val in emp_data.items(): if val not in temp: temp.append(val) resultant_dictionary[key] = val print ("After Removing Duplicates : " + str (resultant_dictionary))
Output
The output for the above code will be as below ?
Contents of the dictionary: {'001': 'Ramu', '002': 'Radha', '003': 'Ramu', '004': 'Raghav'} After Removing Duplicates : {'001': 'Ramu', '002': 'Radha', '004': 'Raghav'}
By using dictionary comprehension method
In this approach we will use the dictionary comprehension method of python. The basic idea of this approach is that we create 2 empty dictionaries and we use dictionary comprehension to add the key value pair of given dictionary to one of the new dictionary but in opposite order, which means instead of adding the key and value pair to new dictionary we will add the value and key pair to this new dictionary.
In this way by using the property of dictionary, that it can only have unique pairs we eliminate the pairs which have same value. After this, we will do the same thing with another empty dictionary to reverse the key value pair from 1st dictionary so that it remains in the same order as in the original dictionary.
Algorithm
Create two new empty dictionaries.
By using the dictionary comprehension method copy the key value pair form original dictionary to one of the new dictionary in value key pair order.
Repeat the step 2 for the other new dictionary.
Print the 2nd dictionary.
Example
In the following code we will be implementing the above algorithm and printing the dictionary with all the unique elements.
marks = {'A' : 90, 'B' : 80, 'C' : 70, 'D' : 90, 'E' : 70} print(" The original dictionary is : " + str(marks)) temp = {val : key for key , val in marks.items()} ans = {val : key for key , val in temp.items()} print ("The dictionary after values removal : " + str (ans) )
Output
The output for the above code will be as below ?
The original dictionary is : {'A' : 90, 'B' : 80, 'C' : 70, 'D' : 90, 'E' : 70} The dictionary after values removal: { 'A' : 90, 'B' : 80, 'C' : 70 }
Conclusion
In this article we came to know about ?what dictionaries are in python', where we can use dictionaries. We came to know about 2 different methods to remove the duplicate values in a dictionary and print them. In this first method we kept the values in a list and on iteration through the given dictionary, when we get a repeated value we do not add the key value pair into a new dictionary and if the value encountered is new we add it.
In the second method we reverse the key value pairs from original dictionary to a new dictionary so that all duplicate values get removed, then we again reverse this new value key pairs into another dictionary and then print it at last.
The time complexity of both approaches is O (n).