How to remove duplicate values from dict?

后端 未结 4 1529
星月不相逢
星月不相逢 2020-12-12 01:52

I\'m trying to remove duplicate values in my dict but its not working:

samples_antibiotics_with_duplicates = {\'S00541-09\': [\'Streptomycin\', \'Sulfamethox         


        
4条回答
  •  抹茶落季
    2020-12-12 02:22

    The below dict comprehension will create a new dict from the original one without any duplicate values:

    samples_antibiotics = {k: list(set(v)) for k, v in samples_antibiotics_with_duplicates.items()}
    

    The set version of a list (or any container) does not contain any duplicates since sets do not allow any (that is why they require hashable items as do dicts).

    As @CoryKramer says in the comments, the solution given here will not (generally speaking) preserve the order of the items in the values-list. If that is important to you, you would have to go with something else.

提交回复
热议问题