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

Alex Martelli aleax at aleax.it
Thu Jan 10 09:04:42 EST 2002


"pekka niiranen" <krissepu at vip.fi> wrote in message
news:3C3D9508.40001 at vip.fi...
> Of course you are right about the syntax of my handwritten example.
>
>  This list is cut from idle's output:
>
> L = [('?AA?BB!CC!', '?BB!'), ('?DD!', ''), ('?EE!', ''), ('?FF?GG!HH!',
'?GG!')]
>
> The resulting list should be:
>
> P = ['?AA?BB!CC!', '?BB!', '?DD!', '?EE!','?FF?GG!HH!' '?GG!']
>
> As you can see, I would not like to have empty values in my list.
> Maybe filter(None, L) can be used.

Sure, but there's no need for that.  The single function I already
suggested can be easily adapted to these more detailed specs -- using
Python 2.2 now, for variety:

def pekka(L):
    seen = {}
    P = []
    for atuple in L:
        for astring in atuple:
            if astring and not astring in seen:
                seen[astring] = 1
                P.append(astring)
    return P


Alex






More information about the Python-list mailing list