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

Kalle Svensson kalle at gnupung.net
Thu Jan 10 08:58:19 EST 2002


[Skip Montanaro]
> 
>     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]

Why the extra append?  To me
P = [x for t in L for x in t if x]
is more obvious.

Peace,
  Kalle
-- 
Kalle Svensson (kalle at gnupung.net) - Laziness, impatience, hubris: Pick two!
English: http://www.gnupung.net/  Svenska: http://www.lysator.liu.se/~kalle/
Stuff: ["http://www.%s.org/" % x for x in "gnu debian python emacs".split()]




More information about the Python-list mailing list