w3resource

Python Exercise: Remove duplicates from Dictionary

Python dictionary: Exercise-17 with Solution

Write a Python program to remove duplicates from Dictionary.

Sample Solution:-

Python Code:

student_data = {'id1': 
   {'name': ['Sara'], 
    'class': ['V'], 
    'subject_integration': ['english, math, science']
   },
 'id2': 
  {'name': ['David'], 
    'class': ['V'], 
    'subject_integration': ['english, math, science']
   },
 'id3': 
    {'name': ['Sara'], 
    'class': ['V'], 
    'subject_integration': ['english, math, science']
   },
 'id4': 
   {'name': ['Surya'], 
    'class': ['V'], 
    'subject_integration': ['english, math, science']
   },
}

result = {}

for key,value in student_data.items():
    if value not in result.values():
        result[key] = value

print(result)

Sample Output:

{'id2': {'subject_integration': ['english, math, science'], 'class': ['V'], 'name': ['David']}, 'id4': {'subje
ct_integration': ['english, math, science'], 'class': ['V'], 'name': ['Surya']}, 'id1': {'subject_integration'
: ['english, math, science'], 'class': ['V'], 'name': ['Sara']}}  

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to get a dictionary from an object's fields.
Next: Write a Python program to check a dictionary is empty or not.

What is the difficulty level of this exercise?



Inviting useful, relevant, well-written and unique guest posts