Is there any nice way to unpack a list of unknown size??
Grant Edwards
grante at visi.com
Sun Sep 14 20:20:10 EDT 2008
On 2008-09-14, Fredrik Lundh <fredrik at pythonware.com> wrote:
>> 1. first, second, third, *rest = foo
>>
>> 2. for (a,b,c,*rest) in list_of_lists:
>
> update to Python 3.0 (as others have pointed out), or just do
>
> first, second, third = foo[:3]
> rest = foo[3:]
Of course you can do that in one line if you want it to look a
bit more like the original pseudocode:
(a,b,c),rest = foo[:3],foo[3:]
That still requires you to manually count the number of
"non-rest" destination elements on the LHS and type that number
twice on the RHS. If you wanted to elminate a tiny bit of the
redundancy you could define a split() function:
def split(seq,n):
return seq[:n],seq[n:]
(a,b,c),rest = split(foo,3)
--
Grant
More information about the Python-list
mailing list