Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@howardhamilton
Last active November 3, 2016 15:50
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 howardhamilton/df47130a645f26d71e6fe1aefd038d7d to your computer and use it in GitHub Desktop.
Save howardhamilton/df47130a645f26d71e6fe1aefd038d7d to your computer and use it in GitHub Desktop.
Unique list of dictionaries
# 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