[Python-Dev] Tuple/list assignment question

Dave Cole djc at object-craft.com.au
Wed Aug 4 06:36:56 CEST 2004


Greg Ewing wrote:
> Nick Coghlan <ncoghlan at iinet.net.au>:
> 
> 
>>Whereas the list assignment proposal gives special synactic sugar to 
>>*one* form of slicing (x[0], ..., x[n], x[(n+1):]).
> 
> 
> It's a comparatively common one, though, I think.  When doing things
> like parsing input, it's often natural to want to peel off the first
> few items of a list and leave the rest for processing later, based on
> what the initial items turn out to be.
> 
> Having to explicitly slice off the part you want to unpack not only
> seems inefficient (constructing an intermediate list or tuple just to
> immediately unpack it and throw it away) it also tends to obfuscate
> what is going on.

So I suppose you could avoid the unnecessary tuple construction by doing 
something like this:

 >>> a_list = [1, 2, 3, 4, 5]
 >>> a, b, * = a_list

 >>> lol = [[1, 2], [3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13]]
 >>> for a, b, * in lol:
...

But that looks a bit bogus - but does avoid having to create the extra 
tuple.

- Dave

-- 
http://www.object-craft.com.au


More information about the Python-Dev mailing list