multiple assignment

Anand anand.chittu at
Wed Mar 22 10:08:34 CET 2006

Suppose i have a big list and i want to take tke the first one and rest
of the list like car/cdr in lisp.
is there any easy way to do this in python?

Only way i know is

a = range(10)
x, y = a[0], a[1:]

In perl, it is possible to do multiple assignment like this....

@a = (1, 2, 3);
($x, @y) = @a;

I find this is a good feature and missing in python.
Why can't python support something like this:

x, *y = a

* operator is used in the usual sence here.

This is not really a new concept to python, infact when calling a
function which takes variable  arguments, it is used in a similar way.
for example:

def f(x, *y): print x, y
f(1, 2, 3)

the argument passing here is very similar to the multiple assignment x,
*y = (1, 2, 3)

any comments?


More information about the Python-list mailing list