Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Remove duplicates and value with None from the list

Hello I have bulk of list of dict and would like to remove duplicate dict and value with None.
Within the data structure, bunch dict are duplicate. I want to keep pair of devices.

BulkData

[
    {"name": "PAIR-05|PAIR-06", "device": "oob-01"},
    {"name": "PAIR-05|PAIR-06", "device": "oob-01"},
    {"name": "PAIR-01|PAIR-02", "device": "oob-03"},
    {"name": "PAIR-01|PAIR-02", "device": "oob-03"},
    {"name": None, "device": "oob-01"},
    {"name": None, "device": "oob-01"},
    {"name": None, "device": "oob-01"},
    {"name": None, "device": "oob-01"},
]

Non-working code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for key, value in bulkpairdata.items():
  t = []
  if bulkpairdata.get("name") != None:
    if bulkpairdata.get("name") not in t:
      result.append(bulkpairdata)
      t.append(bulkpairdata.get("name"))
    else:
      bulkpairdata.remove(bulkpairdata.get("name"))

Result:

[
    {"name": "PAIR-05|PAIR-06", "device": "oob-01"},
    {"name": "PAIR-01|PAIR-02", "device": "oob-03"},
]

>Solution :

You can try something like this:

bulk_data = [
    {"name": "PAIR-05|PAIR-06", "device": "oob-01"},
    {"name": "PAIR-05|PAIR-06", "device": "oob-01"},
    {"name": "PAIR-01|PAIR-02", "device": "oob-03"},
    {"name": "PAIR-01|PAIR-02", "device": "oob-03"},
    {"name": None, "device": "oob-01"},
    {"name": None, "device": "oob-01"},
    {"name": None, "device": "oob-01"},
    {"name": None, "device": "oob-01"},
]

result = []

for item in bulk_data:
    any_none = any([True if value is None else False for key, value in item.items()])
    if not any_none and item not in result:
        result.append(item)

which yields following result:

[{'name': 'PAIR-05|PAIR-06', 'device': 'oob-01'}, {'name': 'PAIR-01|PAIR-02', 'device': 'oob-03'}]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading