Unique Elements in a List

Fredrik Lundh fredrik at pythonware.com
Tue May 10 03:28:27 EDT 2005


Paul Rubin wrote:

> > Is there an easy way to grab the Unique elements from a list?
> > For Example:
> > data = [0.1,0.5,0.6,0.4,0.1,0.5,0.6,0.9]
>
> Untested: here's an iterator that gives you the index and value,
> without reordering.  Uses dicts instead of sets for backwards compatibility.
>
> def unique_elements(data):
>     seen = {}
>     for n,x in data:
>        if x not in seen:
>           seen[x] = 1
>           yield n,x

you forgot enumerate()

and if you fix that, you'll notice that the output doesn't quite match
the OP's spec:

    > For Example:
    > data = [0.1,0.5,0.6,0.4,0.1,0.5,0.6,0.9]
    >
    > what I am looking for is the unique elements 0.4 and 0.9 with their
    > index from the list.

here's a straight-forward variation that gives the specified output,
in the original order:

    def unique_elements(data):
        count = {}
        data = list(enumerate(data))
        for n,x in data:
            count[x] = count.setdefault(x, 0) + 1
        for n,x in data:
            if count[x] == 1:
                yield x, n # value with index

depending on the data, it might be more efficient to store the
"last seen index" in a dictionary, and sort the result on the way
out (if necessary).  something like

    from operator import itemgetter

    def unique_elements(data):
        seen = {}; index = {}
        for n, x in enumerate(data):
            if x in seen:
                del index[x]
            else:
                index[x] = seen[x] = n
        index = index.items()
        index.sort(key=itemgetter(1)) # leave this out if order doesn't matter
        return index

could work.

</F>






More information about the Python-list mailing list