Unique Elements in a List

James Stroud jstroud at mbi.ucla.edu
Mon May 9 18:57:54 EDT 2005


On Monday 09 May 2005 03:15 pm, superprad at gmail.com wrote:
> Is there an easy way to grab the Unique elements from a list?
> For Example:
> data = [0.1,0.5,0.6,0.4,0.1,0.5,0.6,0.9]
>
> what I am looking for is the unique elements 0.4 and 0.9 with their
> index from the list.
> Probably something like a Hash Table approach!!
> I would like to get this done without unnecessary overhead.And the list
> could be essentially anything strings,floats,int etc...
>
> Or is it already avaliable as an attr to a list or an array?
> I dont remember seeing anything like that.

from sets import Set

data = [0.1,0.5,0.6,0.4,0.1,0.5,0.6,0.9]

[x for x in Set(data) if data.count(x) == 1]

-- 
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/



More information about the Python-list mailing list