Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to remove duplicate elements from a dictionary
In this article, we will discuss how to remove duplicate elements from a dictionary in Python. A dictionary stores key-value pairs where keys must be unique, but values can be duplicated. Sometimes we need to clean up data by keeping only unique values.
We can declare a dictionary in the following way ?
sample_dict = {"brand": "Ford", "model": "Mustang", "year": 1964}
print(sample_dict)
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
A dictionary can contain duplicate values for different keys, but we might want to have only unique values. Let's explore two methods to remove duplicate values from a dictionary.
Using Dictionary Iteration
This approach iterates through the dictionary and tracks seen values using a list. We only keep key-value pairs where the value hasn't been encountered before.
Algorithm
- Create a new empty dictionary and a list to track seen values
- Iterate through the original dictionary
- Check if the current value exists in the seen values list
- If not seen, add the key-value pair to the new dictionary and value to the list
- Return the new dictionary with unique values
Example
emp_data = {'001': 'Ramu', '002': 'Radha', '003': 'Ramu', '004': 'Raghav'}
print("Original dictionary:", emp_data)
seen_values = []
unique_dict = {}
for key, value in emp_data.items():
if value not in seen_values:
seen_values.append(value)
unique_dict[key] = value
print("After removing duplicates:", unique_dict)
Original dictionary: {'001': 'Ramu', '002': 'Radha', '003': 'Ramu', '004': 'Raghav'}
After removing duplicates: {'001': 'Ramu', '002': 'Radha', '004': 'Raghav'}
Using Dictionary Comprehension
This approach uses dictionary comprehension with a double reversal technique. First, we swap keys and values (eliminating duplicates due to unique key requirement), then swap back to restore the original structure.
Algorithm
- Create a temporary dictionary with values as keys and keys as values
- Since dictionary keys must be unique, duplicate values are automatically removed
- Reverse the key-value pairs again to restore original structure
- Return the final dictionary
Example
marks = {'A': 90, 'B': 80, 'C': 70, 'D': 90, 'E': 70}
print("Original dictionary:", marks)
# Swap key-value pairs to remove duplicates
temp = {value: key for key, value in marks.items()}
print("Temporary swapped dict:", temp)
# Swap back to original structure
result = {value: key for key, value in temp.items()}
print("After removing duplicates:", result)
Original dictionary: {'A': 90, 'B': 80, 'C': 70, 'D': 90, 'E': 70}
Temporary swapped dict: {90: 'D', 80: 'B', 70: 'E'}
After removing duplicates: {'D': 90, 'B': 80, 'E': 70}
Comparison
| Method | Time Complexity | Space Complexity | Preserves First Occurrence |
|---|---|---|---|
| Dictionary Iteration | O(n) | O(n) | Yes |
| Dictionary Comprehension | O(n) | O(n) | No (keeps last) |
Conclusion
Both methods effectively remove duplicate values from dictionaries with O(n) time complexity. Use dictionary iteration when you need to preserve the first occurrence of duplicate values, or dictionary comprehension for a more concise solution that keeps the last occurrence.
