w3resource

Python: 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']}}  

Pictorial Presentation:

Python Dictionary: Remove duplicates from Dictionary.

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?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

Python: Tips of the Day

Curries a function:

Example:

from functools import partial

def tips_curry(fn, *args):
  return partial(fn,*args)
add = lambda x, y: x + y
add1 = tips_curry(add, 20)

print(add1(80))

Output:

100