Hey I have dictionary like following one dicts = {'met_293': ['81.0175','4','7','7','29.76','23','1','0','22','28.57','2','[KG]EHY'],'met_394': ['79.9579','4','7','7','29.76','18','3','0','15','28.57','2','EHY[ILV]'],'met_309': ['81.0175','4','7','7','29.76','23','1','0','22','28.57','2','[KG]EHY'],'met_387': ['79.9579','4','7','7','29.76','18','3','0','15','28.57','2','EHY[ILV]']}

I want to remove keys which have same value like 'met_293' and 'met_309' those two keys have same value at 12th position which is '[KG]EHY' so i want my dictionary like this

{'met_293': ['81.0175','4','7','7','29.76','23','1','0','22','28.57','2','[KG]EHY'],'met_394': ['79.9579','4','7','7','29.76','18','3','0','15','28.57','2','EHY[ILV]']}

any help!!! thanks

Recommended Answers

All 2 Replies

Here is one example:

import pprint as pp

mydic = {
'a123': [1, 2, 2, 4],
'a124': [1, 2, 3, 4],
'a125': [1, 2, 2, 4],
'a126': [1, 2, 4, 5]
}

newdic = {}
val_list = []
for key, val in mydic.items():
    if val not in val_list:
        newdic[key] = val
    val_list.append(val)

pp.pprint(newdic, width=50)

''' output -->
{'a123': [1, 2, 2, 4],
 'a124': [1, 2, 3, 4],
 'a126': [1, 2, 4, 5]}
'''
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.