Last active
November 3, 2016 15:50
-
-
Save howardhamilton/df47130a645f26d71e6fe1aefd038d7d to your computer and use it in GitHub Desktop.
Unique list of dictionaries
This file contains hidden or 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
# unique list of dictionaries | |
# --------------------------- | |
# This problem comes up when I bulk insert records into databases | |
# because I can't check for existence of records (they don't exist yet, too slow, etc) | |
# | |
# I typically use a set to make list elements unique, but dicts and lists themselves | |
# can't be inserted in sets because they're mutable and thus unhashable. | |
# | |
# Solution is to make a tuple of the dict's items and insert it in set, | |
# then create dicts from each member of the set. | |
_set = set() | |
w = dict(a=2,b=3,c=4) | |
x = dict(a=6,b=4,c=2) | |
y = dict(c=4,a=2,b=3) | |
z = dict(b=7,c=8,a=9) | |
_set.add(tuple(w.items())) | |
_set.add(tuple(x.items())) | |
_set.add(tuple(y.items())) | |
_set.add(tuple(z.items())) | |
records = [dict(elements) for elements in _set] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment