[Python-Dev] Tuple/list assignment question

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


Martin v. Löwis wrote:
> Greg Ewing wrote:
> 
>> 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.
> 
> 
> Of course, the proposed syntax does not change this. Compare
> 
> a,b,c = foo()[:3]

That is not really the use case for the feature.  What I would really 
like is a nice way to avoid doing this:

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

I think that the original is a nicer looking syntax, and is fairly 
consistent with the existing "varargs" handling in function signatures.

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

> to
> 
> a,b,c,*_ = foo()
> 
> In either case, the extra fields obfuscate things, and in either
> case, a second tuple is created. In the latter case, the tuple
> is even stored in a variable.

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


More information about the Python-Dev mailing list