Uniquifying a list?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Chase

    #1

    Uniquifying a list?

    Is there an obvious/pythonic way to remove duplicates from a
    list (resulting order doesn't matter, or can be sorted
    postfacto)? My first-pass hack was something of the form
    [color=blue][color=green][color=darkred]
    >>> myList = [3,1,4,1,5,9,2,6 ,5,3,5]
    >>> uniq = dict([k,None for k in myList).keys()[/color][/color][/color]

    or alternatively
    [color=blue][color=green][color=darkred]
    >>> uniq = list(set(myList ))[/color][/color][/color]

    However, it seems like there's a fair bit of overhead
    here...creating a dictionary just to extract its keys, or
    creating a set, just to convert it back to a list. It feels
    like there's something obvious I'm missing here, but I can't
    put my finger on it.

    Thanks...

    -tkc





  • Edward Elliott

    #2
    Re: Uniquifying a list?

    You could do
    [color=blue][color=green][color=darkred]
    >>> uniq = [x for x in set(myList)][/color][/color][/color]

    but that's not really any different than what you already have.

    This almost works:
    [color=blue][color=green][color=darkred]
    >>> uniq = [x for x in myList if x not in uniq][/color][/color][/color]

    except the r-val uniq isn't updated after each iteration.

    Personally I think list(set(myList )) is as optimal as you'll get.


    Tim Chase wrote:[color=blue]
    > Is there an obvious/pythonic way to remove duplicates from a list
    > (resulting order doesn't matter, or can be sorted postfacto)? My
    > first-pass hack was something of the form
    >[color=green][color=darkred]
    > >>> myList = [3,1,4,1,5,9,2,6 ,5,3,5]
    > >>> uniq = dict([k,None for k in myList).keys()[/color][/color]
    >
    > or alternatively
    >[color=green][color=darkred]
    > >>> uniq = list(set(myList ))[/color][/color]
    >
    > However, it seems like there's a fair bit of overhead here...creating a
    > dictionary just to extract its keys, or creating a set, just to convert
    > it back to a list. It feels like there's something obvious I'm missing
    > here, but I can't put my finger on it.
    >
    > Thanks...
    >
    > -tkc
    >
    >
    >
    >
    >[/color]

    Comment

    • John Machin

      #3
      Re: Uniquifying a list?

      On 19/04/2006 1:31 AM, Tim Chase wrote:[color=blue]
      > Is there an obvious/pythonic way to remove duplicates from a list
      > (resulting order doesn't matter, or can be sorted postfacto)?[/color]

      Google is your friend:


      Comment

      • Rene Pijlman

        #4
        Re: Uniquifying a list?

        Tim Chase:[color=blue]
        >Is there an obvious/pythonic way to remove duplicates from a
        >list (resulting order doesn't matter,[/color]

        Use a set.
        The official home of the Python Programming Language


        --
        René Pijlman

        Comment

        • bearophileHUGS@lycos.com

          #5
          Re: Uniquifying a list?

          There is my version too:


          Bye,
          bearophile

          Comment

          Working...