BMI Students

Saturday, February 11, 2006

Uniquifying a list in Python

I keep needing to do this all the time (given a list, remove all the duplicates). So a one-liner:
dict().fromkeys(yourlist).keys()

4 Comments:

  • Also in python 2.4
    list(set(my_list))
    or stably (from Serge)
    [m for i,m in enumerate(my_list) if m not in my_list[:i]]

    By Blogger brian, at 1:07 PM  

  • Also:
    uniquify = 30k hits
    uniquefy = 251 hits

    That's weird. Neither are words, of course. I blame the word liquid.

    By Blogger brian, at 1:12 PM  

  • Tim Peters wrote a recipe for this in the Python Cookbook that handles various boundary cases, such as when the items are not hashable:
    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52560

    By Blogger jchang, at 3:14 PM  

  • to maintain original order:

    new_list = list(set(orig_list))
    new_list.sort(cmp= lambda x,y: cmp(orig_list.index(x), orig_list.index(y)))

    what's easiest way to preserve indents in source code in Blogger? (Maybe i like ruby better)

    By Blogger E, at 7:28 AM  

Post a Comment

<< Home