Last active
April 29, 2017 11:45
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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