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'}]
]
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'}]]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.