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

Huaiyu Zhu huaiyu at gauss.almadan.ibm.com
Fri Jan 11 03:44:15 CET 2002


On 10 Jan 2002 06:49:28 -0800, Paul Rubin <phr-n2002a at nightsong.com> wrote:
>pekka niiranen <krissepu at vip.fi> writes:
>> I have a list of equal size tuples, but a tuple may contain empty values:
>> 
>> t = [ ( a,b,c), (d, ,f) (b,a,j) ]
>> 
>> How can I convert it into a single list of unique values:
>> 
>> l = [a,b,c,d,f,j]
>> 
>> No lambdas, please
>
>I don't think (d, ,f) is syntactically valid.  Other than that,
>is something wrong with 
>   import operator
>   l = operator.add(*t)


t = [ ( 'a','b','c'), ('d','f'), ('b','a','j') ]

import operator
# Use reduce for more than two arguments
l = list(reduce(operator.add, t))  

# For uniqueness 
l.sort()
def uniq(l, a):
   if [a] != l[-1:]: l.append(a)
   return l

l = reduce(uniq, l, [])


Huaiyu



More information about the Python-list mailing list