def uniq(val, key=None):
    """Produce the unique list.  If val is a dict, produce unique of key values.

       >>> sorted(uniq([1,2,3,2]))
       [1, 2, 3]

       >>> sorted(uniq([ {"a":1}, {"a":2}, {"a":3}, {"a":2}]))
       [{'a': 1}, {'a': 2}, {'a': 3}]

       >>> sorted(uniq([ {"a":1}, {"a":2}, {"a":3}, {"a":2}, Exception()], "a"))
       [{'a': 1}, {'a': 2}, {'a': 3}, Exception()]
    """
    if not isinstance(val, list):
        return val
    if key is None:
        try:
            return list(set(val))
        except TypeError:
            pass
    keys = []
    values = []
    for value in val:
        try:
            thiskey = value[key]
        except:
            thiskey = repr(value)
        if thiskey not in keys:
            keys.append(thiskey)
            values.append(value)
    return values