Dedupe and sort a list in Python 2.2 -


in python 2.2 (don't ask), what's neatest way sort list , remove duplicates?

i can write function sort() iterate, wondering if there's idiomatic one-liner.

edit: list short, efficiency not concern. also, elements immutable.

for old python versions, , since you're using strings, there's no one-liner can think of, pattern this, using dictionaries:

def sorted_uniq(your_list):     table = {}     s in your_list:         table[s] = none     k = table.keys()     k.sort()     return k 

adapted ancient activestate code snippet thread alex martelli himself wrote several comments on: http://code.activestate.com/recipes/52560/

a shorter way list comprehensions:

def sort_uniq(alist):    d = {}    mod_list = [d.setdefault(i,i) in alist if not in d]    mod_list.sort()    return mod_list 

aside steven's neat (yet unattractive) 1 liner, think heads toward fewest lines , idiomatic way of doing python 2.2:

thanks steven rumbalski in comments, 2nd version can condensed further python's zip function:

def sort_uniq(alist):    mod_list = dict(zip(alist,alist)).keys()    mod_list.sort()    return mod_list 

if list.sort() didn't operate side effect, we'd have 1 liner. ;)


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -