do you master list comprehensions?

aleks90210 at hotmail.com aleks90210 at hotmail.com
Fri Dec 17 07:55:43 EST 2004


Thought you might enjoy my super-small flatten function: (though google
groups seems to be munging my whitespace today)

def flatten(d):
"flatten([[[1,[2,3],[4,5]],6],[7]])==[1,2,3,4,5,6,7]"
return reduce(lambda a,b:a+b,[(type(x) in (list, tuple) \
and flatten(x) or [x]) for x in d])




More information about the Python-list mailing list