473,473 Members | 2,236 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

pulling set of unique values from a list

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?

Jul 18 '05 #1
3 3892
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
Jul 18 '05 #2

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

Jul 18 '05 #3
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 && ma*@alcyone.com && 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
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: kevin parks | last post by:
hi. I've been banging my head against this one a while and have asked around, and i am throwing this one out there in the hopes that some one can shed some light on what has turned out to be a...
22
by: Claudio Jolowicz | last post by:
Is it possible to store unique objects in an STL container? Suppose an object of class C is unique: class C { public: C() {} ~C() {} private:
5
by: Marie | last post by:
Access97 I have a table containing addresses with a separate field for State. Is there a way to create a query that returns an unique list of the states in that table and still be updateable? I...
1
by: jason | last post by:
NEWBIE - Hello. I've been able to do the following in other languages (perl,jscript,etc.) but am a asp.net newbie and need some help. I have this textbox that has a bunch of data in it. At the...
1
by: johngilmer | last post by:
I have a dropdown list where the text that the user sees is unique, but the values behind the options in the list are not unique. So the text/value pairs in the list might be: a - 1 b - ...
9
by: Brian Tkatch | last post by:
I'm looking for a simple way to unique an array of strings. I came up with this. Does it make sense? Am i missing anything? (Testing seems to show it to work.) Public Function Unique(ByVal...
1
by: Asko Telinen | last post by:
Hi all. I´m a bit newbie writing xml schemas. Is it possible to define xml element that must have unique attribute values in same level. For example if i have a xml - document: <list>...
0
by: Gabriel Genellina | last post by:
En Fri, 18 Apr 2008 12:23:08 -0300, Shawn Milochik <Shawn@Milochik.comescribió: A dictionary with keys is perfectly reasonable. But a *list* of values has to be searched linearly for every...
5
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I have a list of type object. The object has an ID as one of the elements and I would like to create another list that just has objects with unique IDs. For example in the list if I have...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.