[Python-Dev] syntactic shortcut - unpack to variably sized list

Nick Coghlan ncoghlan at iinet.net.au
Sat Nov 13 13:05:23 CET 2004


Johan Hahn wrote:
> Hi
> 
> As far as I can tell from the archive, this has not been discussed before.
> This is the second time in less than a week that I have stumbled over the rather 
> clumsy syntax of extracting some elements of a sequence and at the same time 
> remove those from the sequence:
> 
>>>>L = 'a b 1 2 3'.split(' ')
>>>>a,b,L = L[0], L[1], L[2:]
> 
> 
> I think it would be nice if the following was legal:
> 
>>>>a,b,*L = 'a b 1 2 3'.split(' ')
>>>>a, b, L
> 
> ('a', 'b', ['1', '2', '3'])

Hmm - I just had a thought about this. Is it worth adding a "len" 
argument to list.pop? (The idea was inspired by Martin's use of list.pop 
to handle the above case).

With that change, the above example would become:

a, b = L.pop(0, 2)

At the moment, list.pop is described as equivalent to:

x = L[i]; del L[i]; return x

with this change, it would be:

x = L[i:i+n]; del L[i:i+n]; return x

By default, n = 1, so the standard behaviour of list.pop is preserved.

Cheers,
Nick.

-- 
Nick Coghlan               |     Brisbane, Australia
Email: ncoghlan at email.com  | Mobile: +61 409 573 268


More information about the Python-Dev mailing list