Mike
Mike

Reputation: 683

An interesting code for obtaining unique values from a list

Say given a list s = [2,2,2,3,3,3,4,4,4]

I saw the following code being used to obtain unique values from s:

unique_s = sorted(unique(s))

where unique is defined as:

def unique(seq): 
    # not order preserving 
    set = {}
    map(set.__setitem__, seq, []) 
    return set.keys()

I'm just curious to know if there is any difference between this and just doing list(set(s))? Both results in a mutable object with the same values.

I'm guessing this code is faster since it's only looping once rather than twice in the case of type conversion?

Upvotes: 0

Views: 1243

Answers (2)

jfs
jfs

Reputation: 414625

For a sorted sequence you could use itertools unique_justseen() recipe to get unique values while preserving order:

from itertools import groupby
from operator import itemgetter

print map(itemgetter(0), groupby([2,2,2,3,3,3,4,4,4]))
# -> [2, 3, 4]

To remove duplicate items from a sorted sequence inplace (to leave only unique values):

def del_dups(sorted_seq):
    prev = object()
    pos = 0
    for item in sorted_seq:
        if item != prev:
            prev = item
            sorted_seq[pos] = item
            pos += 1
    del sorted_seq[pos:]

L = [2,2,2,3,3,3,4,4,4]
del_dups(L)
print L # -> [2, 3, 4]

Upvotes: 1

Dietrich Epp
Dietrich Epp

Reputation: 213578

You should use the code you describe:

list(set(s))

This works on all Pythons from 2.4 (I think) to 3.3, is concise, and uses built-ins in an easy to understand way.

The function unique appears to be designed to work if set is not a built-in, which is true for Python 2.3. Python 2.3 is fairly ancient (2003). The unique function is also broken for the Python 3.x series, since dict.keys returns an iterator for Python 3.x.

Upvotes: 3

Related Questions