What is the cleanest way to do a sort plus uniq on a Python list?

Posted by knorv on Stack Overflow See other posts from Stack Overflow or by knorv
Published on 2010-05-28T18:46:48Z Indexed on 2010/05/28 18:52 UTC
Read the original article Hit count: 165

Filed under:

Consider a Python list my_list containing ['foo', 'foo', 'bar'].

What is the most Pythonic way to uniqify:ing and sorting and the list (think cat my_list | sort | uniq)?

This is how I currently do it and while it works I'm sure there are better ways to do it.

my_list = []
...
my_list.append("foo")
my_list.append("foo")
my_list.append("bar")
...
my_list = set(my_list)
my_list = list(my_list)
my_list.sort()

© Stack Overflow or respective owner

Related posts about python