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

Skip Montanaro skip at pobox.com
Thu Jan 10 08:40:32 EST 2002


    pekka> This list is cut from idle's output:

    pekka> L = [('?AA?BB!CC!', '?BB!'), ('?DD!', ''), ('?EE!', ''), ('?FF?GG!HH!', '?GG!')]

    pekka> The resulting list should be:

    pekka> P = ['?AA?BB!CC!', '?BB!', '?DD!', '?EE!','?FF?GG!HH!' '?GG!']

How about (using list comprehensions ==> version >= 2.0 required):

    P = []
    [P.append(x) for t in L for x in t if x]

The result of the list comprehension is useless.  The side effect of
executing P.append is what's useful:

    >>> P = []
    >>> [P.append(x) for t in L for x in t if x]
    [None, None, None, None, None, None]
    >>> P
    ['?AA?BB!CC!', '?BB!', '?DD!', '?EE!', '?FF?GG!HH!', '?GG!']

-- 
Skip Montanaro (skip at pobox.com - http://www.mojam.com/)




More information about the Python-list mailing list