Skip to content

Instantly share code, notes, and snippets.

@renzon
Created December 15, 2016 16:25
Show Gist options
  • Save renzon/b36ddd8737d8fddc38a805040a40274c to your computer and use it in GitHub Desktop.
Save renzon/b36ddd8737d8fddc38a805040a40274c to your computer and use it in GitHub Desktop.
remove os elementos duplicatos de uma lista, mesmo que sejam dicionários
def unique_list(lst):
backtrack = set()
def hash_dicts(dct):
if isinstance(dct, dict):
return str(sorted((k, hash_dicts(v)) for k, v in dct.items()))
return str(dct)
def is_repeated(dct):
hashable = hash_dicts(dct)
if hashable in backtrack:
return True
backtrack.add(hashable)
return False
return [dct for dct in lst if not is_repeated(dct)]
print(unique_list(
[
{'action': 'allow', 'source': {}, 'destination': {}},
{'action': 'allow', 'source': {}, 'destination': {}},
{'action': 'allow', 'source': {}, 'destination': {}},
{'action': 'allow', 'source': {'foo': 1, 'bar': 2}, 'destination': {}},
{'action': 'allow', 'source': {'bar': 2, 'foo': 1}, 'destination': {}}
]
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment