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()

2 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  

Post a Comment

<< Home