Remove Duplicate Value From Dictionary Without Removing Key
Solution 1:
You have the right overall idea, but there are three problems with your code:
- You're storing values back into
myDict
instead of intonewDict
. - On line 2, you're checking
values
instead ofvalue
. - Also on line 2,
key
shouldn't be there, and throws aSyntaxError
.
Here is the correct code:
newDict = {}
for key, value in myDict.iteritems():
if value not in newDict.values():
newDict[key] = value
else:
newDict[key] = ""print newDict
If you're not in the anti-ternary operator camp, you could also shorten it to this:
newDict = {}
for key, value in myDict.iteritems():
newDict[key] = value if value not in newDict.values() else""print newDict
Or, if you would rather just remove the values from the original dict
(myDict
) instead of building a new one (newDict
), you could do this:
foundValues = []
forkey, value in myDict.iteritems():
if value notin foundValues:
foundValues.append(myDict[key])
else:
myDict[key] = ""
print myDict
If you need duplicate values removed in a specific order, check out OrderedDict
s.
Update:
In light of the updated requirements -- that values be removed from the original dict
, starting from the beginning -- if you're able to simply initialize myDict
with an OrderedDict
instead of a dict
, all you need to do is replace this:
myDict = {'key1': ['item1', 'item2', 'item3'], 'key2': ['item4', 'item5', 'item6'], 'key3': 'item7', 'key4': 'item8', 'key5': ['item1', 'item2', 'item3'], 'key6': 'item7'}
with this:
from collections importOrderedDict
…
myDict = OrderedDict([('key1', ['item1', 'item2', 'item3']), ('key2', ['item4', 'item5', 'item6']), ('key3', 'item7'), ('key4', 'item8'), ('key5', ['item1', 'item2', 'item3']), ('key6', 'item7')])
and then use the same code provided above.
Solution 2:
This does it:
myDict_values = myDict.values() # better than calling this numerous times
for key in myDict.keys():
if myDict_values.count(myDict[key]) > 1: myDict[key] = ""
This won't guarantee that key5
will be blank instead of key1
, because dictionaries are not ordered.
Post a Comment for "Remove Duplicate Value From Dictionary Without Removing Key"