Is there any nice way to unpack a list of unknown size??

Tim Chase python.list at tim.thechases.com
Sun Sep 14 15:16:04 EDT 2008


>> I want to do something like below:
>> 1. first, second, third, *rest = foo
>
> Python 3.0 has exactly this feature.  No current Python 2.x version has it.

I asked something similar[1] on c.l.p a while back and Diez 
Roggisch gave this nice workaround/hack[2]

It's a bit ugly in the implementation (sniffing the stack), but 
elegant in the use, doing exactly what I needed.

It doesn't accommodate the "rest" portion that you reference, but 
allows for arbitrary unpacking into a fixed-length tuple.

-tkc


[1]
http://mail.python.org/pipermail/python-list/2006-May/381386.html

[2]
http://mail.python.org/pipermail/python-list/2006-May/381399.html







More information about the Python-list mailing list