How to convert list of tuples into a single list of unique values ?

Alex Martelli aleax at aleax.it
Thu Jan 10 06:59:26 EST 2002


"pekka niiranen" <krissepu at vip.fi> wrote in message
news:3C3D7AFB.8050505 at vip.fi...
> I have a list of equal size tuples, but a tuple may contain empty values:
>
> t = [ ( a,b,c), (d, ,f) (b,a,j) ]

This is not valid Python syntax.  There is no "empty value" that is
denoted by two adjacent commas in the displayform of a tuple.  Maybe
you mean for the middle element of the middle tuple to be None?  Or
what else?

If the items you list are one-character strings, they'd be 'a', 'b'
and so on in Python.  You seem to be listing variable names.  Is
there a specific meaning attached to this?


> How can I convert it into a single list of unique values:
>
> l = [a,b,c,d,f,j]
>
> No lambdas, please

No lambda needed.  In Python 2.1 or earlier, assuming all the
items in the tuples are hashable (strings, numbers, tuples, ...):

def pekka(listoftuples):
    values_seen = {}
    result = []
    for atuple in listoftuples:
        for anitem in atuple:
            if not values_seen.has_key(anitem):
                values_seen[anitem] = 1
                result.append(anitem)
    return result

In Python 2.2, the innermost if could be coded more clearly:

            if anitem not in values_seen:

but the general structure can be similar (or, one might code a
simple generator to return an iterator rather than a list, but
that may not be of interest to you).

If the items are not hashable, determining uniqueness is far
more delicate.  If the items are comparable (e.g., not complex
numbers) you can maintain a sorted list of the items with
module bisect (running time will be O(N log N), though).  For
total generality, you must unfortunately check exhaustively
(with "if anitem not in result") and get a running time of
O(N squared).  Only you know what amount of generality you
must support, so only you will be able to decide about this.


Alex






More information about the Python-list mailing list