pulling set of unique values from a list

Stephan Diehl stephan.diehlNOSPAM at gmx.net
Wed Jan 14 09:43:18 EST 2004


Ben Davies wrote:

> I need to get [1,2,3] from [1,1,1,2,2,3,3] with as little effort possible
> for my CPU (and keyboard).
> I'd half expected there to be a list.values method to do this, but there
> isn't so I've have had to use a dictionary object:
>
 
I'd use a set for that:

>>> from sets import Set
>>> l = [1,1,1,2,2,3,3]
>>> [x for x in Set(l)]
[1, 2, 3]
>>>

By the way, in a lot of cases where I used lists, I'm now using sets because
they seem more natural in some situations.

Stephan



More information about the Python-list mailing list