Consider a Python list my_list
containing [\'foo\', \'foo\', \'bar\']
.
What is the most Pythonic way to uniquify and sort a list ?
(thi
# Python ≥ 2.4
# because of (generator expression) and itertools.groupby, sorted
import itertools
def sort_uniq(sequence):
return (x[0] for x in itertools.groupby(sorted(sequence)))
Faster:
import itertools, operator
import sys
if sys.hexversion < 0x03000000:
mapper= itertools.imap # 2.4 ≤ Python < 3
else:
mapper= map # Python ≥ 3
def sort_uniq(sequence):
return mapper(
operator.itemgetter(0),
itertools.groupby(sorted(sequence)))
Both versions return an generator, so you might want to supply the result to the list type:
sequence= list(sort_uniq(sequence))
Note that this will work with non-hashable items too:
>>> list(sort_uniq([[0],[1],[0]]))
[[0], [1]]