Skip to content

Instantly share code, notes, and snippets.

@mapcloud
Last active April 29, 2017 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mapcloud/0dc13ead9515f5ab9330431745b6e5f4 to your computer and use it in GitHub Desktop.
Save mapcloud/0dc13ead9515f5ab9330431745b6e5f4 to your computer and use it in GitHub Desktop.
Python: Creates an unique list for a given list of dictionaries. Useful to remove duplicates of dictionaries within a list.
# Credits to: http://stackoverflow.com/questions/11741876/getting-unique-values-from-a-list-of-dict
import ast
def unique_list_in_list_of_dictionaries(content_list: list())->list:
"""
content_list = [{'A':1, 'B':1, 'C':1},
{'A':1, 'B':1, 'C':1},
{'A':2, 'B':2, 'C':2}
{'A':3, 'B':2, 'C':1}
{'A':3, 'B':2, 'C':1}]
:returns:
[{'A':1, 'B':1, 'C':1},
{'A':2, 'B':2, 'C':2},
{'A':3, 'B':2, 'C':1}]
"""
return [ast.literal_eval(el1) for el1 in set([str(el2) for el2 in content_list])]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment