miércoles, 13 de julio de 2011

Remove repeated elements from a list in Python

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()


Taken from the desk of stinkpot

2 comentarios:

  1. The fastest way to uniqify a list in Python is:

    >>> {}.fromkeys(x_nodups).keys()


    Time ago I checked a site with several ways to do this, and this was the chosen ( http://www.peterbe.com/plog/uniqifiers-benchmark )

    ResponderEliminar
  2. Jacen's code:

    import random
    a = []
    for i in range(0,1000000): a.append(int(random.random()*100))
    print dict(map(lambda i: (i,1), a)).keys()

    real 0m2.104s
    user 0m2.000s
    sys 0m0.090s

    Henhiskan's code:

    import random
    a = []
    for i in range(0,1000000): a.append(int(random.random()*100))
    print {}.fromkeys(a).keys()

    real 0m0.838s
    user 0m0.770s
    sys 0m0.020s

    My version:

    import random
    a = []
    for i in range(0,1000000): a.append(int(random.random()*100))
    print list(set(a))

    real 0m0.734s
    user 0m0.690s
    sys 0m0.040s

    Seems faster and less obscure.

    Saludos

    ResponderEliminar