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:
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'}]