Skip to content

Instantly share code, notes, and snippets.

@bungoume
Created January 15, 2015 10:14
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 bungoume/dea503cd07eb55d2cebb to your computer and use it in GitHub Desktop.
Save bungoume/dea503cd07eb55d2cebb to your computer and use it in GitHub Desktop.
python unique function
def uniq(array, callback=None):
seen = []
result = []
for obj in array:
if callback:
key = callback(obj)
else:
key = obj
if key not in seen:
seen.append(key)
result.append(obj)
return result
@bungoume
Copy link
Author

>>> uniq([{'id':1},{'id':2},{'id':2}], lambda x: x['id'])
[{'id':1},{'id':2}]
>>> uniq([1,2,2,3])
[1,2,3]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment