Thursday, 15 August 2013

Remove duplicate value from dictionary without removing key

Remove duplicate value from dictionary without removing key

I'm new to Python and am having to learn by trial and error, but I haven't
been able to find a solution to the problem I'm having.
I have a dictionary that looks something like this:
myDict = {'key1': ['item1', 'item2', 'item3'], 'key2': ['item4', 'item5',
'item6'],
'key3': 'item7', 'key4': 'item8', 'key5': ['item1', 'item2', 'item3'],
'key6': 'item7'}
I need to remove duplicate values from the dictionary and replace them
with an empty value (""). Found a couple solution on here but they are
working as intended
for key, value in myDict.items():
if values not in key newDict.values():
myDict[key] = value
else:
myDict[key] = ""
print newDict
This is removing all the values and is outputting
# newDict:{key1: '', key2: '', key3: '', key4: '', key5: '', key6: '')

No comments:

Post a Comment