Skip to content

Instantly share code, notes, and snippets.

@naotokui
Last active May 10, 2017 05:55
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 naotokui/309dab1384548a80fc03013895f387ab to your computer and use it in GitHub Desktop.
Save naotokui/309dab1384548a80fc03013895f387ab to your computer and use it in GitHub Desktop.
list -> set in python remove duplication while preserving the original order
a = [5, 1, 3, 5, 4, 2, 10, 7, 8, 0, 1, 3]
# list -> set : remove duplication
print list(set(a)) # [0, 1, 2, 3, 4, 5, 7, 8, 10]
# list -> set while preserving original order
b = sorted(set(a), key=a.index) # [5, 1, 3, 4, 2, 10, 7, 8, 0]
# if the list is very very long and performance matters, use more_itertools
# pip install more_itertools
from more_itertools import unique_everseen
b = list(unique_everseen(a))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment