Flatten a list/tuple and Call a function with tuples

Stargaming stargaming at gmail.com
Wed Jul 25 17:19:40 CEST 2007

On Wed, 25 Jul 2007 14:50:18 +0000, beginner wrote:

> Hi,
> I am wondering how do I 'flatten' a list or a tuple? For example, I'd
> like to transform[1, 2, (3,4)] or [1,2,[3,4]] to  [1,2,3,4].

A recursive function, always yielding the first element of the list, 
could do the job. See the ASPN Python Cookbook for a few implementations.

> Another question is how do I pass a tuple or list of all the aurgements
> of a function to the function. For example, I have all the arguments of
> a function in a tuple a=(1,2,3). Then I want to pass each item in the
> tuple to a function f so that I make a function call f(1,2,3). In perl
> it is a given, but in python, I haven't figured out a way to do it.
> (Maybe apply? but it is deprecated?)

>>> def foo(a, b, c): print a, b, c
>>> t = (1, 2, 3)
>>> foo(*t)
1 2 3

Have a look at the official tutorial, 4.7.4 http://www.python.org/doc/

> Thanks,
> cg


More information about the Python-list mailing list