Skip to content

Latest commit

 

History

History
33 lines (27 loc) · 959 Bytes

removing_dupes_from_dicts.md

File metadata and controls

33 lines (27 loc) · 959 Bytes

Removing duplicate values from keys in a dict

Sometimes you need to remove duplicate values from a dictonary for example take a sellers.json file:

"sellers": [
    {
      "seller_id": "539521906",
      "name": "JustPremium BV",
      "domain": "justpremium.com",
      "seller_type": "INTERMEDIARY",
      "is_passthrough": 0
    },
    {
      "seller_id": "540402547",
      "name": "JustPremium BV",
      "domain": "justpremium.com",
      "seller_type": "INTERMEDIARY",
      "is_passthrough": 0
    },
    ...
]

Looping through the above would give you 2 justpremium.com what if you wanted just one?

You can easily remove duplicate keys by dictionary comprehension, since dictionary does not allow duplicate keys, as below:

unique = { each['domain'] : each for each in sellers }.values()

Found the fix here