Last active
May 10, 2017 05:55
-
-
Save naotokui/309dab1384548a80fc03013895f387ab to your computer and use it in GitHub Desktop.
list -> set in python remove duplication while preserving the original order
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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