Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > pulling set of unique values from a list

Reply
Thread Tools

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?





 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
 
 
 
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

 
Reply With Quote
 
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.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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a unique method in python to unique a list? Token Type Python 9 09-09-2012 02:13 PM
Pulling values from strings Michael Furmaniuk Ruby 4 04-15-2009 12:46 PM
list question... unique values in all possible unique spots ToshiBoy Python 6 08-12-2008 05:01 AM
How to get just unique values from a List<object> Paul ASP .Net 5 08-01-2008 10:35 PM
Is there any way to have a dropdown list with non-unique values? johngilmer@yahoo.com ASP .Net 1 05-12-2005 02:20 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57