Velocity Reviews > pulling set of unique values from a list

# pulling set of unique values from a list

Ben Davies
Guest
Posts: n/a

 01-14-2004
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:

>>> l=[1,1,1,2,2,2,3,3,3]
>>> dict.fromkeys(l).keys()

[1, 2, 3]

of course another way would be to do it 'by hand' like this:

>>> l=[1,1,1,2,2,2,3,3,3]
>>> values=[]

.... for v in l:
.... if not v in values: values.append(v)
>>> values

[1, 2, 3]

personally I prefer the dictionary one-liner, but with the second iterative
way it is probably more obvious what the code is doing (and perhaps more
efficient?).
Is there any better way to do this that I've missed, I was kind of surprised
not to find a values() method on the built-in list type?

Stephan Diehl
Guest
Posts: n/a

 01-14-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

Skip Montanaro
Guest
Posts: n/a

 01-14-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]

Skip

Erik Max Francis
Guest
Posts: n/a

 01-14-2004
Ben Davies wrote:

> personally I prefer the dictionary one-liner, but with the second
> iterative
> way it is probably more obvious what the code is doing (and perhaps
> more
> efficient?).

No, the dictionary method is more efficient. The list-using algorithm
has O(n^2) behavior, whereas the dictionary-using algorithm is only
O(n).

--
__ Erik Max Francis && http://www.velocityreviews.com/forums/(E-Mail Removed) && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ You can't expect to win unless you know why you lose.
-- Benjamin Lipson