convert list of tuples into several lists

Steven Bethard steven.bethard at gmail.com
Thu Feb 10 02:54:37 EST 2005


Cappy2112 wrote:
> What does the leading * do?

Tells Python to use the following iterable as the (remainder of the) 
argument list:

py> def f(x, y):
...     print x, y
...
py> f([1, 2])
Traceback (most recent call last):
   File "<interactive input>", line 1, in ?
TypeError: f() takes exactly 2 arguments (1 given)
py> f(*[1, 2])
1 2
py> f(1, [2])
1 [2]
py> f(1, *[2])
1 2

Note that whenever the leading * is present, the following list gets 
expanded into the positional arguments of f -- x and y.

Steve



More information about the Python-list mailing list