Reputation: 416
I want to extract unique data from nested list includes dicts, see below. Is there any efficient way to generate a new uniq list
[
[{'port': 18, 'module': 1, 'policy_group': 'policy_group1'}]
[{'port': 10, 'module': 1, 'policy_group': 'policy_group2'}]
[{'port': 10, 'module': 1, 'policy_group': 'policy_group2'}]
]
The new list should be
[
[{'port': 18, 'module': 1, 'policy_group': 'policy_group1'}]
[{'port': 10, 'module': 1, 'policy_group': 'policy_group2'}]
]
Upvotes: 0
Views: 38
Reputation: 24049
You can iterate over the list
and use tuple and set
and then convert items in set
to dict
again like below:
>>> lst = [[{'port': 18, 'module': 1, 'policy_group': 'policy_group1'}],[{'port': 10, 'module': 1, 'policy_group': 'policy_group2'}],[{'port': 10, 'module': 1, 'policy_group': 'policy_group2'}]]
>>> [[dict(item)] for item in set(tuple(l[0].items()) for l in lst)]
[[{'port': 10, 'module': 1, 'policy_group': 'policy_group2'}],
[{'port': 18, 'module': 1, 'policy_group': 'policy_group1'}]]
Upvotes: 2