Feed on
Posts
Comments

say we have the python list:

>>x = [5,6,5]

to remove the duplications, use the following code snippet:

>>x_nodups = dict(map(lambda i: (i,1),x)).keys()

::thanks to walter underwood::


Bookmark and Share

if that was helpful ...

check out the other tips and tricks i've compiled on these pages. you might learn something else interesting!

2 Responses to “find all the unique elements in a python list”

  1. on 24 Oct 2006 at 7:38 pm lawrence

    python 2.4 (i think) and up also has the set type:

    >>> f = [1, 2, 3, 4, 5, 5, 5, 6]
    >>> set(f)
    set([1, 2, 3, 4, 5, 6])
    >>> list(set(f))
    [1, 2, 3, 4, 5, 6]

  2. on 24 Oct 2006 at 7:39 pm Lawrence David

    yea, just heard about that a week or two ago. thanks for posting that info!

Did I get this wrong? Let me know!

Trackback URI | Comments RSS