Syntactic sugar for assignment statements: one value to multiple targets?

Tim Chase python.list at
Wed Aug 3 13:15:46 CEST 2011

On 08/03/2011 03:25 AM, Steven D'Aprano wrote:
> gc wrote:
>> Target lists using comma separation are great, but they don't work
>> very well for this task. What I want is something like
>> a,b,c,d,e = *dict()
> a, b, c, d, e = [dict() for i in range(5)]
> Unfortunately there is no way of doing so without counting the assignment
> targets. While slightly ugly, it doesn't seem ugly enough to justify the
> extra complexity of special syntax for such a special case.

I understand that in Py3k (and perhaps back-ported into later 2.x 
series?) one can do something like

a, b, c, d, e, *junk = (dict() for _ in range(9999))

to prevent the need to count.  However, I was disappointed with 
all the generator-ification of things in Py3k, that this "and the 
rest" syntax slurps up the entire generator, rather than just 
assigning the iterator.  That would much more tidily be written as

a,b,c,d,e, *more_dict_generator = (dict() for _ in itertools.count())

(itertools.count() happening to be a infinite generator).  I can 
see the need to slurp if you have things afterward:

   a,b,c, *junk ,d,e = iterator(...)

but when the "and the rest" is the last one, it would make sense 
not to force a slurp.


More information about the Python-list mailing list