In this python dictionary program, we will remove duplicate values from dictionary values.
Steps to solve the program
- Take a dictionary as input and create an empty dictionary.
- Use for loop to iterate over keys and values of the dictionary.
- Use a nested for loop to iterate over the value list.
- Using an if statement check for duplicate values.
- If any duplicate value exists then remove it from the value list using remove().
- Add the key and the value list to the empty dictionary.
- Print the output.
dict1 = {'marks1':[23,28,23,69],'marks2':[ 25, 14, 25]}
for key,val in dict1.items():
for ele in val:
if ele in val:
val.remove(ele)
print(dict1)
Output :
{'marks1': [28, 69], 'marks2': [14, 56]}