pulling set of unique values from a list

Discussion in 'Python' started by Ben Davies, Jan 14, 2004.

  1. Ben Davies

    Ben Davies Guest

    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?
     
    Ben Davies, Jan 14, 2004
    #1
    1. Advertising

  2. 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
     
    Stephan Diehl, Jan 14, 2004
    #2
    1. Advertising

  3. 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
     
    Skip Montanaro, Jan 14, 2004
    #3
  4. 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
     
    Erik Max Francis, Jan 14, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    724
    Patrice
    May 12, 2005
  2. ToshiBoy
    Replies:
    6
    Views:
    888
    ToshiBoy
    Aug 12, 2008
  3. Michael Furmaniuk

    Pulling values from strings

    Michael Furmaniuk, Apr 14, 2009, in forum: Ruby
    Replies:
    4
    Views:
    172
    Michael Furmaniuk
    Apr 15, 2009
  4. RedGrittyBrick

    Pulling pairs of values from an array

    RedGrittyBrick, Dec 17, 2010, in forum: Perl Misc
    Replies:
    3
    Views:
    173
  5. Token Type
    Replies:
    9
    Views:
    393
    Chris Angelico
    Sep 9, 2012
Loading...

Share This Page