pulling set of unique values from a list

Skip Montanaro skip at pobox.com
Wed Jan 14 15:48:31 CET 2004

    Ben> I need to get [1,2,3] from [1,1,1,2,2,3,3] with as little effort
    Ben> possible for my CPU (and keyboard).

You didn't say if order was important, but since one solution you posted was
dict-based, I'll assume not.  If you're running 2.3 or from CVS, this seems
the most straightforward to me:

    >>> list(set([1,1,1,2,2,3,3]))
    [1, 2, 3]

given the new set type.  If you are running 2.3 you'll need to import from
the sets module:

    >>> from sets import Set as set
    >>> list(set([1,1,1,2,2,3,3]))
    [1, 2, 3]


More information about the Python-list mailing list