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

Nick Coghlan ncoghlan at iinet.net.au
Sat Nov 13 14:41:25 CET 2004

Martin v. Löwis wrote:
> Nick Coghlan wrote:
>> 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.
> This default would actually change the standard behaviour: whereas it
> now returns a single element, it would then return a list containing
> the single element.

Ah, good point.

I'd see two possible fixes to that:

a) have n=0 be the default, and mean 'give me the element, not a list 
with 1 element" (L.pop(0,1) would then mean "give me a list containing 
only the first element"). That's a little magical for my taste, though.

b) have a new method called 'extract' or 'poplist' or 'popslice' or 
similar (with the behaviour given above)

Actually, if we went with the b) option, and the name 'popslice', I 
would suggest the following signature:

list.popslice(start=0, end, step=1)

i.e. L.popslice(a, b, c) is to L[a:b:c] as L.pop(a) is to L[a]

And, returning once again to the OP's example, we would have:

a, b = list.popslice(2)


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

More information about the Python-Dev mailing list