Flatten a list/tuple and Call a function with tuples

Jeff jeffober at gmail.com
Wed Jul 25 15:34:47 EDT 2007


On Jul 25, 3:05 pm, "Eduardo \"EdCrypt\" O. Padoan"
<eopad... at altavix.com> wrote:
> def flatten(listOfLists):
>     return list(chain(*listOfLists))
>
> >Fromhttp://www.python.org/doc/2.4/lib/itertools-recipes.html
>
> --
> EduardoOPadoan (eopadoan->altavix::com)
> Bookmarks:http://del.icio.us/edcrypt

That doesn't necessarily work:

import itertools

x = (1, 2, [3, 4, (5, 6)])
y = ([1, 2, (3, 4)], 5, 6)
z = (1, [2, 3, (4, 5)], 6)

def flatten(listOfLists):
    return list(itertools.chain(*listOfLists))

print flatten(x)
print flatten(y)
print flatten(z)

==> TypeError: chain argument #1 must support iteration




More information about the Python-list mailing list