Skip to content

Instantly share code, notes, and snippets.

@rectangletangle
Created May 26, 2015 21:35
Show Gist options
  • Save rectangletangle/61ffdaad36c60e77fa3b to your computer and use it in GitHub Desktop.
Save rectangletangle/61ffdaad36c60e77fa3b to your computer and use it in GitHub Desktop.
import uuid
import random
def ordered_uniquification(items):
seen = set()
for item in items:
if not item in seen:
seen.add(item)
yield item
if __name__ == '__main__':
total = 100000
original_emails = [str(uuid.uuid1()) + '@example.com'
for _ in range(int(total / 2))] # Should be xrange in Python 2.*
emails = original_emails * 2
random.shuffle(emails)
unique_emails = list(ordered_uniquification(emails))
assert len(unique_emails) == len(original_emails)
# A far less efficient one-liner that basically does the same thing
# (don't worry, I don't normally write code like this lol)
assert unique_emails == [email for _, email in
sorted((index, email)
for email, index in
{email: i
for i, email in reversed(list(enumerate(emails)))}.items())]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment