In Python 2.2 (don’t ask), what’s the neatest way to sort a list and remove duplicates?
I can obviously write a function that would sort() then iterate, but am wondering if there’s an idiomatic one-liner.
edit: The list is short, so efficiency is not a concern. Also, the elements are immutable.
For old python versions, and since you’re using strings, there’s no one-liner I can think of, but a pattern would probably be this, using dictionaries:
Adapted from an ancient ActiveState code snippet thread that Alex Martelli himself wrote several comments on: http://code.activestate.com/recipes/52560/
A shorter way with list comprehensions:
Aside from Steven’s neat (yet slightly unattractive) one liner, I think this heads toward the fewest lines and most idiomatic way of doing it with Python 2.2:
Thanks to Steven Rumbalski in the comments, the 2nd version can be condensed further with python’s
zipfunction:If
list.sort()didn’t operate by side effect, we’d have a one liner. 😉