how to separate a list into two lists?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Aug 6 20:13:02 EDT 2011


bud wrote:

> I'll have to check up on the *L - is that a reference?

No, as Chris already answered, unary * is used for packing and unpacking
positional arguments to functions; unary ** is similarly used for
collecting keyword arguments.


> I know in Perl, you can assign the lhs to a list,
> below works because there are exactly 2 items on the rhs.
> Does Python have a catchall, or an ignore the rest?

Yes. In Python 3, you can do this:

>>> a, b, *t, c = (1, 2, 3, 4, 5, 6, 7, 8, 9)
>>> t
[3, 4, 5, 6, 7, 8]

(but not in Python 2)


-- 
Steven




More information about the Python-list mailing list