views:

1738

answers:

5

Given a list of strings, I want to sort it alphabetically and remove duplicates. I know I can do this:

from sets import Set
[...]
myHash = Set(myList)

but I don't know how to retrieve the list members from the hash in alphabetical order.

I'm not married to the hash, so any way to accomplish this will work. Also, performance is not an issue, so I'd prefer a solution that is expressed in code clearly to a fast but more opaque one.

A: 

here's a nice recipe.

bchhun
Works, but too verbose when you don't care about performance.
jmglov
+21  A: 
sorted(set(myList))

set is a native datatype for Python >= 2.3

Rod Daunoravicius
Perfect blend of expressiveness and brevity. Thanks, Rod!
jmglov
Although set is available in 2.3+, sorted was added in Python 2.4
Jeff Bauer
Jeff, you're right of course. If sorted is not available: no_dups = list(set(myList)); no_dups.sort()
Rod Daunoravicius
A: 

If it's clarity you're after, rather than speed, I think this is very clear:

def sortAndUniq(input):
  output = []
  for x in input:
    if x not in output:
      output.append(x)
  output.sort()
  return output

It's O(n^2) though, with the repeated use of not in for each element of the input list.

unwind
I prefer the solution using set due to the brevity it affords without sacrificing clarity.
jmglov
+1  A: 

If your input is already sorted, then there may be a simpler way to do it:

from operator import itemgetter
from itertools import groupby
unique_list = list(map(itemgetter(0), groupby(yourList)))
sykora
This can also be expressed as [e for e, _ in groupby(sortedList)]
Rafał Dowgird
+1  A: 

> but I don't know how to retrieve the list members from the hash in alphabetical order.

Not really your main question, but for future reference Rod's answer using sorted can be used for traversing a dict's keys in sorted order:

for key in sorted(my_dict.keys()):
   print key, my_dict[key]
   ...

and also because tuple's are ordered by the first member of the tuple, you can do the same with items:

for key, val in sorted(my_dict.items()):
    print key, val
    ...
davidavr