[Python][Def] Check for Unique Values Based an a Key from a List of Dicts

The following Python definition can be used to check for unique values, based on a key, from a list of dictionary items.

def checkUnique(*arg):
  # Check for unique values based on a key, from a list of dicts.
  # Usage: checkUnique(LIST,DICT_KEY)
  list = arg[0]
  key = arg[1]
  i = []
  for l in list: i += [l[key]]
  if len(i) > len(set(i)):
    return ("Not unique: {0} > {1}".format(len(i),len(set(i))))
  else:
    return ("Unique: {0} = {1}".format(len(i),len(set(i))))




Comments